javascript - jQuery on click for two buttons in same div -


i have site displays bar graphs of data. trying implement pagination graphs user can click 'next' or 'previous' scroll through different subsets of total data.

here html section in question:

<div class="graph_fields_wrap1 row backdrop col-lg-12">     <div class="col-lg-6">         <h3 class="titles">top ten author citations</h3>         <h4 class="titles">all time (from 1970)</h4>         <button class="pager" id="previous1" type="button"><span class="glyphicon glyphicon-chevron-left"></span> previous</button>         <button class="pager" id="next1" type="button">next <span class="glyphicon glyphicon-chevron-right"></span></button>         <div class="chart1 bs-component"></div>     </div>     <div class="col-lg-6">         <h3 class="titles">top ten author citations</h3>         <h4 class="titles usertitle"></h4>         <button class="pager" id="previous2" type="button"><span class="glyphicon glyphicon-chevron-left"></span> previous</button>         <button class="pager" id="next2" type="button">next <span class="glyphicon glyphicon-chevron-right"></span></button>         <div class="chart2 bs-component"></div>     </div> </div> <!-- row --> 

here javascript:

$(document).ready(function() {     // change graph according author selection     var wrapperg = $(".graph_fields_wrap1"); // wrapper div containing citations graphs     var next1 = $("#next1"); // pagination time cited graph     var previous1 = $("#previous1");     var next2 = $("#next2"); // pagination user defined cited graph     var previous2 = $("#previous2");     // variables log subset location in arrays (to use in slice)     var from1 = 1;     var to1 = 11;      // pagination //     // time cited, next author set     $(wrapperg).on("click", next1, function (e) {         // ignore default action event         e.preventdefault();         // shift pointers 10 next subset of array         from1 += 10;         console.log(from1);         to1 += 10;         console.log(to1);         // remove displayed graph, 1st child of div (1st graph 0th)         $($(wrapperg).children()[0]).remove();         // load new graph before other graph (1st child of div)         $(wrapperg).prepend("<div class='col-lg-6'><h3 class='titles'>top ten author citations</h3><h4 class='titles'>all time (from 1970)</h4><button class='pager' id='previous1' type='button'><span class='glyphicon glyphicon-chevron-left'></span> previous</button><button class='pager' id='next1' type='button'>next <span class='glyphicon glyphicon-chevron-right'></span></button><div class='chart1 bs-component'></div></div>").loadgraph((topcited.slice(from1,to1)), "chart1", palette1);     });      // time cited, previous author set     $(wrapperg).on("click", previous1, function (e) {         // ignore default action event         e.preventdefault();         // shift pointers down 10 previous subset of array         from1 -= 10;         console.log(from1);         to1 -= 10;         console.log(to1);         // remove displayed graph, 1st child of div (1st graph 0th)         $($(wrapperg).children()[0]).remove();         // load new graph before other graph (1st child of div)         $(wrapperg).prepend("<div class='col-lg-6'><h3 class='titles'>top ten author citations</h3><h4 class='titles'>all time (from 1970)</h4><button class='pager' id='previous1' type='button'><span class='glyphicon glyphicon-chevron-left'></span> previous</button><button class='pager' id='next1' type='button'>next <span class='glyphicon glyphicon-chevron-right'></span></button><div class='chart1 bs-component'></div></div>").loadgraph((topcited.slice(from1,to1)), "chart1", palette1);     }); }); 

i used $(next1).on("click", function button fired once stopped working. looking similar queries on stack overflow saw that, because html removed, bound handlers well. binded handler part not removed (wrapperg).

i put console.log lines me see what's going on when click buttons. when load page , click 'next', console logs data 11, 21, 1, 11 , keeps repeating data on repeat clicks of either 'next' or 'previous' button click. on clicking 'next' want data log 11, 21 21, 31 31, 41 , on. 'previous' button decreasing 10 each time. bar graph shows 10 bars of data according in array slice taken.

if comment out 'previous' button jquery section 'next' button works fine , bar graph displays data properly. makes me think problem in both buttons being contained within same div. i'm removing child()[0] of .graph_fields_wrap1 within function div can refer to.

** additional **

listing console.log data above realised i'd copied out incorrectly. returning 11, 21, 1, 11.

this localizes problem being when clicking 'next' button, firing both event handlers, first 1 increases values 10, second 1 decreases them 10, negating effect.

therefore, need find out why clicking 'next' button firing off both events , how stop happening.

the problem use same element id twice. prevent double events add "return false" handler, or use differnt id's

sidenode: never use id twice instead use class


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -