javascript - How can I use jquery events on a list of ids? -


i have html list of ids like:

<div id="id0" value="myval0">click me</div> <div id="id1" value="myval1">click me</div> <div id="id2" value="myval2">click me</div> <div id="id3" value="myval3">click me</div> 

the list can generated server. trying jquery of form send server id has been clicked:

$( "#id0" ).click(function() {   var urlstring = '/getresult'; alert($("#id0").val());   $.ajax({     type: "post",     url: urlstring,     data: {       draft_no: $("#id0").val(),     },     success: function(result){       alert("function returned success!")     }   }); }); 

in case have sent id of form "id0". how can generalise "id n" n integer?

try jquery selector e[a^=v] match elements e possess attribute a value starts width v

your html:

<div id="id-1" value="my val 1">click me</div> <div id="id-2" value="my val 2">click me</div> <div id="id-3" value="my val 3">click me</div> 

your jquery

$('div[id^=id]').click(function() {   var urlstring = '/getresult';   var val = $(this).val();   $.ajax({     type: "post",     url: urlstring,     data: {       draft_no: val,     },     success: function(result){       alert("function returned success!")     }   }); }); 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -