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

1 Answer 1

1

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

Use document.getElementById instead of depending on globals.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.