/    Sign up×
Community /Pin to ProfileBookmark

Script will only run once?

I’ve got a pretty simple javascript function that is called when you click a button. The script pulls a value from a dropdown box on the page and uses that value to determine what text to plug into a textarea text box. The script runs perfectly for all options the first time you click the button. However, if you click the button again nothing happens. I don’t even get an error message in the Web Developer add-on in FireFox. It seems as though it’s not even running the script. Any ideas?

HTML Code:

[CODE]
<select id=”drpCodeTemplates” name=”drpCodeTemplates” size=”1″ tabindex=”11″>
<option selected value=”NA”></option>
<option value=”Featured”>Featured</option>
<option value=”RSS”>RSS</option>
</select>

<input type=”button” name=”CodeTemplatesButton” value=”Add Code” tabindex=”12″ onclick=”AddCode();”>

<textarea name=”txtDefinition” id=”txtDefinition” rows=”15″ cols=”95″ tabindex=”13″><% Response.Write Server.HTMLEncode(strDefinition) %></textarea>
[/CODE]

Javascript Code:

[CODE]
function AddCode(){
var strCodeTemplate = document.getElementById(“drpCodeTemplates”).value;
var strDefinition = document.getElementById(“txtDefinition”).value;

if(strDefinition.length == 0){
switch(strCodeTemplate){
case “Featured” :
document.getElementById(“txtDefinition”).innerHTML = “Featured”;
break;
case “RSS” :
document.getElementById(“txtDefinition”).innerHTML = “RSS”;
break;
default :
if(strCodeTemplate == “NA”){
document.getElementById(“txtDefinition”).innerHTML = “Code Template Operation Failed. DeeDeeDee! You have to choose a template for this to work!”;
}
else{
document.getElementById(“txtDefinition”).innerHTML = “Code Template Operation Failed. Please inform the webmaster.”;
}
}
}
else{
alert(“You must first delete all text from the Definition text box in order to add a code template”);
}
}
[/CODE]

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERFeb 17.2009 — Have you changed the selection of the drop-down box between button clicks?
Copy linkTweet thisAlerts:
@nbcrockettauthorFeb 18.2009 — Yes, the only way to get the script to run again is to leave the page and then come back.
Copy linkTweet thisAlerts:
@JMRKERFeb 19.2009 — I don't understand all the logic of what you are trying to do,

but hopefully this is close:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled&lt;/title&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?p=980214#post980214

function AddCode(){
var strCodeTemplate = document.getElementById("drpCodeTemplates").value;
var strDefinition = document.getElementById("txtDefinition").value;

<i> </i> switch(strCodeTemplate){
<i> </i> case "NA" :
<i> </i> document.getElementById("txtDefinition").innerHTML
<i> </i> = "Code Template Operation Failed. DeeDeeDee! You have to choose a template for this to work!";
<i> </i> break;
<i> </i> case "Featured" :
<i> </i> document.getElementById("txtDefinition").innerHTML = "Featured";
<i> </i> break;
<i> </i> case "RSS" :
<i> </i> document.getElementById("txtDefinition").innerHTML = "RSS";
<i> </i> break;
<i> </i> case "Clear" :
<i> </i> document.getElementById("txtDefinition").innerHTML = "";
<i> </i> break;
<i> </i> default :
<i> </i> alert("You must first delete all text from the Definition text box in order to add a code template");
// document.getElementById("txtDefinition").innerHTML
// = "Code Template Operation Failed. Please inform the webmaster.";
break;
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;select id="drpCodeTemplates" name="drpCodeTemplates" size="1" tabindex="11"&gt;
&lt;option selected value="NA"&gt;NA&lt;/option&gt;
&lt;option value="Featured"&gt;Featured&lt;/option&gt;
&lt;option value="RSS"&gt;RSS&lt;/option&gt;
&lt;option value="Clear"&gt;Clear&lt;/option&gt;
&lt;/select&gt;

&lt;input type="button" name="CodeTemplatesButton" value="Add Code" tabindex="12" onclick="AddCode();"&gt;

&lt;textarea name="txtDefinition" id="txtDefinition" rows="15" cols="95" tabindex="13"&gt;
&lt;!-- % Response.Write Server.HTMLEncode(strDefinition) % --&gt;
&lt;/textarea&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@nbcrockettauthorFeb 19.2009 — Ok, that worked a little better. However, you took out the code that kept the script from inserting text if something was already in the textarea. I need some way to keep people from overwriting text that's already there. After updating my code to yours I was able to add a template then change my choice and add another template, but if I typed anything in the textarea the script wouldn't run anymore. Even if I deleted what I typed it still wouldn't run.

Code Removed:
[CODE]
if(strDefinition.length == 0){
[COLOR="Red"]The code you left in your example goes here.[/COLOR]
}
else{
alert("You must first delete all text from the Definition text box in order to add a code template");
}
[/CODE]
Copy linkTweet thisAlerts:
@JMRKERFeb 19.2009 — Again: I don't understand all the logic of what you are trying to do,[/QUOTE]
but you could try adding [COLOR="Magenta"]readonly[/COLOR] as an argument to the <textarea>
Copy linkTweet thisAlerts:
@nbcrockettauthorFeb 20.2009 — Let me see if I can write this a little clearer.

I've got a textarea box like the one that I'm typing this post in right now. I want my users to be able to type in the box, but I'm trying to make their job a little easier by giving them a way to add large amounts of text to the box. This text is a template of commonly used text formats that we use. Think of it as a Microsoft Word Template. I know the text in the sample code I posted was only a few words, but that was just text I was using to test the script.

Now the problem:

If I put my cursor in the text box the script doesn't run.

Latest Code:
[CODE]
function AddCode(){
var strCodeTemplate = document.getElementById("drpCodeTemplates").value;
var strDefinition = document.getElementById("txtDefinition").value;

if(strDefinition.length == 0){
switch(strCodeTemplate){
case "NA" :
document.getElementById("txtDefinition").innerHTML = "Code Template Operation Failed. DeeDeeDee! You have to choose a template for this to work!";
break;
case "Featured" :
document.getElementById("txtDefinition").innerHTML = "Featured Template";
break;
case "RSS" :
document.getElementById("txtDefinition").innerHTML = "RSS Template";
break;
default :
document.getElementById("txtDefinition").innerHTML = "Code Template Operation Failed. Please inform the webmaster.";
}
}
else{
alert("You must first delete all text from the Definition text box in order to add a code template");
}
}
[/CODE]
Copy linkTweet thisAlerts:
@JMRKERFeb 20.2009 — Are you trying to execute the code withing the 'txtDefinition' box?

The stuff being displayed as .innerHTML?

Are you over writing the text or do you want to append to the existing text?
Copy linkTweet thisAlerts:
@nbcrockettauthorFeb 20.2009 — Your comment about innerHTML made me realize I should be using value instead. I switched and now it works.

In this case the text is the first thing that gets put into the box, but I have another function I want to add that will add text where your cursor is in the text box. Do you know how to do that?

Thanks for all of your help!
Copy linkTweet thisAlerts:
@JMRKERFeb 20.2009 — Appending to the area is easy. Just do += instead of = to change the display
<i>
</i> if(strDefinition.length == 0){
// var oldstr = document.getElementById("txtDefinition").innerHTML; // or .value ???

<i> </i> switch(strCodeTemplate){
<i> </i> case "NA" :
<i> </i> document.getElementById("txtDefinition").innerHTML += "Code Template Operation Failed. DeeDeeDee! You have to choose a template for this to work!";
<i> </i> break;
<i> </i> case "Featured" :
<i> </i> document.getElementById("txtDefinition").innerHTML += "Featured Template";
<i> </i> break;
<i> </i> case "RSS" :
<i> </i> document.getElementById("txtDefinition").innerHTML += "RSS Template";
<i> </i> break;
<i> </i> default :
<i> </i> document.getElementById("txtDefinition").innerHTML += "Code Template Operation Failed. Please inform the webmaster.";
<i> </i> }
}
else{
alert("You must first delete all text from the Definition text box in order to add a code template");
}
}


Entering at cursor is more difficult. I think I have see it done before, but I'll need to look around. In meantime, try searching this forum with terms like 'add text at cursor" or some other significant words.
×

Success!

Help @nbcrockett spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...