Get an object value in jquery or javascript and makes a post request -


well problem this, lets take example. there 30 links in page, , under each link there button. , when clicks button link liked via ajax post request php file.

but possible using jquery or javascript write single set of code find link liked (by getting id or of link) , send php via ajax request, rather writing 30 lines of ajax post request each link being liked. thanks

you can associate each link button using data-* attribute. on button, use data-mylink="linkid" , give link's id.

then, on onclick can this:

$('.buttonclass').on('click', function(){     var mylinkid = $(this).attr('data-mylink');      //as david barker suggested, can grab $(this).data('mylink')     //whatever else have do. }); 

alternatively, wrap them both in div , do:

$('.buttonclass').on('click', function(){     $(this).parent().find('a').text() or .val() or .attr('href'); }); 

edit

as war10ck pointed out, if items created dynamically, you'll want delegate with:

$(document).on('click', '.buttonclass', function () {      // code here  }); 

what says is: use click button exists class="buttonclass" or button created later on class="buttonclass".


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -