[Solved] Onclick working only once

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

[Solved] Onclick working only once

Post 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]';	
}
Last edited by social_experiment on Mon Jun 20, 2011 11:34 am, edited 1 time in total.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Onclick working only once

Post 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)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Onclick working only once

Post by social_experiment »

Thanks :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: [Solved] Onclick working only once

Post 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]';        
} 
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply