Reputation: 520
i am having a loop of textarea
which has diferent values and a corresponding button
to each textarea.
What i need to do is, if i click a button the value in the respective textarea should be alerted using javascript.
Here is the code what i tried:
<?php
$a=0;
$b=100;
while($a<5)
{
echo "<textarea id='a'>".$b."</textarea><br/>";
echo "<button onClick='ddd(a.value)'>click</button><br>";
$a++;
$b++;
}
?>
<script>
function ddd(d)
{
alert(d);
}
</script>
But it shows same value (first textarea value) on alert to all button click.
So i changed the id of textarea to dynamic id as
echo "<textarea id='$a'>".$b."</textarea><br/>";
echo "<button onClick='ddd($a.value)'>click</button><br>";
But it is not working. It haven't even give any alert message.
How can i achieve this? Is there any other ways? Suggest me..
Upvotes: 0
Views: 686
Reputation: 943579
A JavaScript identifier cannot start with a number, and $a
will always be a number.
Use document.getElementById
instead of depending on globals.
Upvotes: 1