Jump to content

onclick with variables..


MDanz

Recommended Posts


<?php $number= "1";
$text1 = "Hello";

?>

<div style='display:none;' id='$number'>$text1</div>

<img src='http://www.hgdhhd.com/test.jpg' alt='test!' onclick='ChgText($number)' />

 

 

<script type='text/javascript'>
function ChgText(number){

   var newtext = document.getElementById(number).innerHTML
   document.changer.reply.value += newtext;
}
</script>


<textarea name='info' cols='50' rows='10' wrap='hard' id='reply'></textarea>

 

 

 

onClick it should say Hello in the textarea but its not.. any help..

Link to comment
Share on other sites

Hi,

You may well actually have different code from what you have pasted here but if your code is actually like this your first problem is that you are not actually writing the value of the variable $text in the hidden layer as you have closed the php tags.

You should do something like this:

<div style='display:none;' id='<?php echo $number; ?>'><?php echo $text1; ?></div>
<img src='http://www.hgdhhd.com/test.jpg' alt='test!' onclick='ChgText(<?php echo $number; ?>)' />

 

Secondly, in the javascript you are telling it to write to an element with the name "reply".

There is no element with that name in your code.

You have

<textarea name='info' cols='50' rows='10' wrap='hard' id='reply'></textarea>

So, either modify your javascript to use the name "info" or, preferable in my opinion, change it to reference the element id using getElementById() like this:

document.changer.getElementById('reply').value += newtext;

 

Chris

 

Link to comment
Share on other sites

thanks for the help.. i tried this and its not working...where i go wrong?

 

<?php $number= "1";
$text1 = "Hello";

?>

<div style='display:none;' id='<?php echo $number; ?>'><?php echo $text1; ?></div>

<script type='text/javascript'>
function ChgText(number){

   var newtext = document.getElementById(number).innerHTML
   document.changer.getElementById('reply').value += newtext;
}
</script>
<img src="http://www.u-stack.com/quote.jpg" alt="test!" onclick="ChgText('<?php echo $number; ?>')" />

<textarea name="reply" cols="50" rows="10" wrap="hard" id="reply"></textarea>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.