javascript - Creating elements dynamically and adding functions with different parameters. -


i'm creating anchor tags dynamically , want them call function has set parameter each one.

example:

<div class="anchorholder">    <a onclick="somefunction(1)">sometext</a>    <a onclick="somefunction(2)">sometext</a>    <a onclick="somefunction(3)">sometext</a> </div> 

i've tried

$("#anchorholder").append("<a>" + sometext + "</a>").click(function (e) {     somefunction(someintvariable); }); 

but instead connects of anchors intvariables current value, whereas wanted previous ones.

how can accomplish this?

well, suggest try data attributes. far know made porpuse. see:

$("#anchorholder").append("<a href='#' data-myvar='" + someintvariable + "'>" + sometext + "</a>");  // keep event binding out of loop, considering code above called more once... $("#anchorholder").on('click', 'a', function (e) {     alert($(this).data("myvar")); }); 

fiddle


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -