0

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..

user3784251
  • 520
  • 2
  • 10
  • 24

1 Answers1

1

A JavaScript identifier cannot start with a number, and $a will always be a number.

Use document.getElementById instead of depending on globals.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335