/    Sign up×
Community /Pin to ProfileBookmark

Somebody please help me make a handler

Sure, my case is very simple, but I cant understand it and crush my head several days))
I use method on(), because the elements, which must to listen the event, also must to listen the events on other elements. So the addEventListener is unsuitable here. Say me, please, how make the constantly working handler?
Here thats, about I talking – the handler works two times only:
$(‘#wrapper’).children().children().on(“click”, “div.listener”, function(e) {
$( “div.listener” ).css(“display”, “none”);
$( “#clicker” ).bind(“click”, function(e) {
$( “div.listener” ).css(“display”, “block”);
})
});
$( “#clicker” ).bind(“click”, function(e) {
$( “div.listener” ).trigger(“click”);
});
Here the all code – may to do testing for clarity:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd“>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<title>&#1044;&#1086;&#1082;&#1091;&#1084;&#1077;&#1085;&#1090; &#1073;&#1077;&#1079; &#1085;&#1072;&#1079;&#1074;&#1072;&#1085;&#1080;&#1103;</title>
<script src=”js/jquery-1.11.3.js”></script>
<script type=”text/javascript”>

$(document).ready(function(){

var allFormBlocks = document.getElementsByClassName(“formblock”);
var hiddenArr = $(allFormBlocks);
hiddenArr.slice(1).hide();

$(“div.listener”).css(“color”, “red”);

$(‘#wrapper’).children().children().on(“click”, “div.listener”, function(e) {
$( “div.listener” ).css(“display”, “none”);
$( “#clicker” ).bind(“click”, function(e) {
$( “div.listener” ).css(“display”, “block”);
})
});
$( “#clicker” ).bind(“click”, function(e) {
$( “div.listener” ).trigger(“click”);
});

$(‘#button1’).bind(“click”, function(e){//&#1082;&#1086;&#1085;&#1077;&#1094; &#1073;&#1083;&#1086;&#1082;&#1072;1 &#1089;&#1083;&#1077;&#1076;&#1091;&#1102;&#1097;&#1080;&#1081; &#1088;&#1072;&#1079;&#1076;&#1077;&#1083;
$(‘#block1’).hide();
$(‘#block2’).show();
});

$(‘#button2’).bind(“click”, function(e){//&#1082;&#1086;&#1085;&#1077;&#1094; &#1073;&#1083;&#1086;&#1082;&#1072;2 &#1089;&#1083;&#1077;&#1076;&#1091;&#1102;&#1097;&#1080;&#1081; &#1088;&#1072;&#1079;&#1076;&#1077;&#1083;
$(‘#block2’).hide();
$(‘#block3’).show();
});

$(‘#button3’).bind(“click”, function(e){//&#1082;&#1086;&#1085;&#1077;&#1094; &#1073;&#1083;&#1086;&#1082;&#1072;2 &#1087;&#1088;&#1077;&#1076;&#1099;&#1076;&#1091;&#1097;&#1080;&#1081; &#1088;&#1072;&#1079;&#1076;&#1077;&#1083;
$(‘#block2’).hide();
$(‘#block1’).show();
});
$(‘#button4’).bind(“click”, function(e){//&#1082;&#1086;&#1085;&#1077;&#1094; &#1073;&#1083;&#1086;&#1082;&#1072;3 &#1087;&#1088;&#1077;&#1076;&#1099;&#1076;&#1091;&#1097;&#1080;&#1081; &#1088;&#1072;&#1079;&#1076;&#1077;&#1083;
$(‘#block3’).hide();
$(‘#block2’).show();
});

});

</script>
</head>
<body>

<div id=”wrapper”>
<div class=”testform”>
<form method=”POST” name=”form”>

<div class=”formblock” id=”block1″>
<h1>&#1069;&#1090;&#1086; &#1073;&#1083;&#1086;&#1082; &#8470;1</h1>
<div>
<p><label><input id=”clicker” type=”checkbox” name=”npo” value=”0″>&#1089;&#1082;&#1088;&#1099;&#1090;&#1100; &#1085;&#1077;&#1082;&#1086;&#1090;&#1086;&#1088;&#1099;&#1077; &#1101;&#1083;&#1077;&#1084;&#1077;&#1085;&#1090;&#1099;<label></p>
</div>
<div>DIV &#1073;&#1083;&#1086;&#1082;&#1072; &#8470;1</div>
<div class=”listener”>&#1069;&#1090;&#1086; &#1090;&#1086;&#1078;&#1077; DIV &#1073;&#1083;&#1086;&#1082;&#1072; &#8470;1</div>
<div>&#1080; &#1101;&#1090;&#1086; &#1090;&#1086;&#1078;&#1077; DIV &#1073;&#1083;&#1086;&#1082;&#1072; &#8470;1</div>
<button type=”button” id=”button1″>&#1089;&#1083;&#1077;&#1076;&#1091;&#1102;&#1097;&#1080;&#1081; &#1088;&#1072;&#1079;&#1076;&#1077;&#1083;</button>
</div>
<div class=”formblock” id=”block2″>
<h1>&#1069;&#1090;&#1086; &#1073;&#1083;&#1086;&#1082; &#8470;2</h1>
<div>DIV &#1073;&#1083;&#1086;&#1082;&#1072; &#8470;2</div>
<div class=”listener”>&#1069;&#1090;&#1086; &#1090;&#1086;&#1078;&#1077; DIV &#1073;&#1083;&#1086;&#1082;&#1072; &#8470;2</div>
<div>&#1080; &#1101;&#1090;&#1086; &#1090;&#1086;&#1078;&#1077; DIV &#1073;&#1083;&#1086;&#1082;&#1072; &#8470;2</div>
<button type=”button” id=”button2″>&#1089;&#1083;&#1077;&#1076;&#1091;&#1102;&#1097;&#1080;&#1081; &#1088;&#1072;&#1079;&#1076;&#1077;&#1083;</button></br>
<button type=”button” id=”button3″>&#1087;&#1088;&#1077;&#1076;&#1099;&#1076;&#1091;&#1097;&#1080;&#1081; &#1088;&#1072;&#1079;&#1076;&#1077;&#1083;</button>
</div>
<div class=”formblock” id=”block3″>
<h1>&#1069;&#1090;&#1086; &#1073;&#1083;&#1086;&#1082; &#8470;3</h1>
<div>DIV &#1073;&#1083;&#1086;&#1082;&#1072; &#8470;3</div>
<div class=”listener”>&#1069;&#1090;&#1086; &#1090;&#1086;&#1078;&#1077; DIV &#1073;&#1083;&#1086;&#1082;&#1072; &#8470;3</div>
<div>&#1080; &#1101;&#1090;&#1086; &#1090;&#1086;&#1078;&#1077; DIV &#1073;&#1083;&#1086;&#1082;&#1072; &#8470;3</div>
<button type=”button” id=”button4″>&#1087;&#1088;&#1077;&#1076;&#1099;&#1076;&#1091;&#1097;&#1080;&#1081; &#1088;&#1072;&#1079;&#1076;&#1077;&#1083;</button>
</div>
</form>
</div>

</div>
</body>
</html>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumFeb 07.2016 — which must to listen the event, also must to listen the events on other elements. Say me, please, how make the constantly working handler?[/QUOTE]You might do this in the following way: Write a function doing all the stuff you need for both elements. Then use this function in the eventhandlers for both elements:
[CODE]function myHandler(ev) {
// do some stuff
}
$("#element1").on("click", myHandler);
$("#element2").on("click", myHandler);[/CODE]
Copy linkTweet thisAlerts:
@__1052___1072___1082_si__1084_authorFeb 08.2016 — Thanks, thats true, but i didnt know the slideToggle() method:

function SH() {

$('#listener').slideToggle();

}

$('#clicer').on('click',SH);
Copy linkTweet thisAlerts:
@rootFeb 08.2016 — Also note that theres a sub forum to JavaScript especially for JQuery and other frameworks, see my signature for a link, and remember when posting any type of code, the forum uses forum tags to wrap code elements to make life easier for those helping you.
×

Success!

Help @__1052___1072___1082_si__1084_ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...