javascript - receive the value from link -


good day everyone. have problem realization 1 situation. have links let 5 (but can more that). every link have id="#link", have 5 different variables. need how receive 1 of variables in jquery depending of link click on. how can realize it?

<?php $a=1; $b=2; $c=3; $d=4; $e=5; printf ("<a href=\"\" id=\"#link\">link_a</a>"); printf ("<a href=\"\" id=\"#link\">link_b</a>"); printf ("<a href=\"\" id=\"#link\">link_c</a>"); printf ("<a href=\"\" id=\"#link\">link_d</a>"); printf ("<a href=\"\" id=\"#link\">link_e</a>"); ?> 

so here 5 variables - a, b c d e, , 5 links, need recive 1 of variables depending of clicked link;

$(document).on('click','#link',function() {  alert(result); return false; }); 

p.s. numbers of variables , links can more 5. can 10 or 20. take 5 example. help.

you have need store data in anchor (html) elements.

data attributes way go.

php code:

<?php $a=1; $b=2; $c=3; $d=4; $e=5; printf ("<a href=\"\" class=\"mylink\" id=\"#link\" data-variable=\"$a\">link_a</a>"); printf ("<a href=\"\" class=\"mylink\" id=\"#link\" data-variable=\"$b\">link_b</a>"); printf ("<a href=\"\" class=\"mylink\" id=\"#link\" data-variable=\"$c\">link_c</a>"); printf ("<a href=\"\" class=\"mylink\" id=\"#link\" data-variable=\"$d\">link_d</a>"); printf ("<a href=\"\" class=\"mylink\" id=\"#link\" data-variable=\"$e\">link_e</a>"); ?> 

ids within html document, should unique. use classes if have need redundant identifiers.

javascript code:

$(document).on('click','a.mylink',function() {     //use .attr() data attribute.     alert($(this).attr('data-variable'));     return false; }); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -