Reputation: 558
Good day everyone. Have a problem with realization one situation. I have some links let say 5 (but it can be more than that). Every link have id="#link", but I also have a 5 different variables. So I need some how receive one of that variables in jquery depending of the link that will be click on. How can I 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 is 5 variables - a, b c d e, and 5 links, so I need to recive one of that variables depending of the clicked link;
$(document).on('click','#link',function() {
alert(result);
return false;
});
P.S. Numbers of variables and links can be more than 5. It can be 10 or 20. But I take 5 for an example. Thanks for your help.
Upvotes: 0
Views: 91
Reputation: 21437
You can't assign same id
to multiple links in order to achieve such functionality you need to use class
Its because id's were need to be unique
<?php
$a=1;
$b=2;
$c=3;
$d=4;
$e=5;
printf ("<a href=\"\" class=\"link\" data-id=\"$a\">link_a</a>");
printf ("<a href=\"\" class=\"link\" data-id=\"$b\">link_b</a>");
printf ("<a href=\"\" class=\"link\" data-id=\"$c\">link_c</a>");
printf ("<a href=\"\" class=\"link\" data-id=\"$d\">link_d</a>");
printf ("<a href=\"\" class=\"link\" data-id=\"$e\">link_e</a>");
?>
JS
$(document).on('click','a.mylink',function() {
alert($(this).attr('data-id'));
//or you can directly use it as
alert($(this).data('id'));
return false;
});
Upvotes: 1
Reputation: 27072
Use class
instead of ID
(or use just a
element) and put PHP variables into any data-attribute.
<?php
$a=1;
$b=2;
$c=3;
$d=4;
$e=5;
printf ("<a href='' class='link' data-value='" . $a ."'>link_a</a>");
printf ("<a href='' class='link' data-value='" . $b ."'>link_a</a>");
printf ("<a href='' class='link' data-value='" . $c ."'>link_a</a>");
printf ("<a href='' class='link' data-value='" . $d ."'>link_a</a>");
printf ("<a href='' class='link' data-value='" . $e ."'>link_a</a>");
?>
<script>
var a = document.getElementsByTagName('a');
a.onclick = function() {
alert(this.getAttribute('data-value'));
return false;
}
</script>
Upvotes: 1
Reputation: 5791
You have a need to store data in the anchor (HTML) elements.
Data attributes is the way to 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 an HTML document, should be unique. Use classes if you have a need for redundant identifiers.
Javascript Code:
$(document).on('click','a.mylink',function() {
//Use .attr() to get the data attribute.
alert($(this).attr('data-variable'));
return false;
});
Upvotes: 1
Reputation: 3859
Modification of you code
<script>
function get(){
var m=$(this).val();
alert(m);
}
</script>
<?php
$a=1;
$b=2;
$c=3;
$d=4;
$e=5;
echo "<a href='' onclick=get() value=".$a." >link_a</a>";
echo "<a href='' onclick=get() value=".$b." >link_b</a>";
echo "<a href='' onclick=get() value=".$c." >link_c</a>";
echo "<a href='' onclick=get() value=".$d." >link_d</a>";
echo "<a href='' onclick=get() value=".$e." >link_e</a>";
?>
It will work for fine as per your requirements.
Upvotes: 1
Reputation: 2820
try
$(document).on('click','#link',function() {
alert($(this).attr('id'));
});
Upvotes: 0