MDanz Posted September 13, 2009 Share Posted September 13, 2009 <?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.. Quote Link to comment https://forums.phpfreaks.com/topic/174101-onclick-with-variables/ Share on other sites More sharing options...
cbolson Posted September 13, 2009 Share Posted September 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174101-onclick-with-variables/#findComment-917752 Share on other sites More sharing options...
MDanz Posted September 13, 2009 Author Share Posted September 13, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/174101-onclick-with-variables/#findComment-917753 Share on other sites More sharing options...
KevinM1 Posted September 13, 2009 Share Posted September 13, 2009 Remove the '.changer' portion of the code, so it reads as 'document.getElementById("reply").value += newtext;' Quote Link to comment https://forums.phpfreaks.com/topic/174101-onclick-with-variables/#findComment-917801 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.