Page 1 of 1

[Solved] Onclick working only once

Posted: Sun Jun 19, 2011 5:28 pm
by social_experiment
I've created code that works similar to the buttons when posting in them forum (Quote, Text, PHP Code, etc). The problem is that once i've clicked on it and remove the newly created text from the textarea, i cannot get it displayed again unless i refresh the page. This is with the following browsers : Firefox, Chrome and Safari. In IE7 it works as i would like it to, each click (without refreshing the page) displays the text (after i've removed the created text). Herewith my code :

Html

Code: Select all

<form action="" method="post" />
<input type="button" name="btn" value="PHP" onclick="codeTags('comment');" />
<textarea id="comment" rows="7" cols="30" /></textarea>
<input type="submit" name="submit" value="Submit" />
And the javascript

Code: Select all

function codeTags(p)
{
 theElement = document.getElementById(p);
 theElement.innerHTML = '[code][/code]';	
}

Re: Onclick working only once

Posted: Sun Jun 19, 2011 5:41 pm
by Weirdan

Code: Select all

function codeTags(p)
{
 var theElement = document.getElementById(p);
 theElement.value = '[code][/code]';        
}
works for me in every browser I have (Chromium, Firefox, IE9, Opera)

Re: Onclick working only once

Posted: Mon Jun 20, 2011 11:33 am
by social_experiment
Thanks :)

Re: [Solved] Onclick working only once

Posted: Mon Mar 26, 2012 4:34 am
by social_experiment
Update
I worked with this code again today and found an issue; the code Weirdan gave works but it will clear textarea of any text already inside it. If updated to the snippet below it behaves appropriately

Code: Select all

function codeTags(p)
{
 var theElement = document.getElementById(p);
 // changed = to += so it can append to instead of clearing any
 // existing data.
 theElement.value += '[code][/code]';        
}