/    Sign up×
Community /Pin to ProfileBookmark

variable not passing right…

so I have this script that adds what is in the list box into the textfield. What the problem is, is it’s not working when i define which textbox to set it to in the listbox code. Here’s the code:

function insertChar(objList, theLoca){
if(intSelected!=0){
if(isIE&&!isMac){
objList.form.theLoca.focus();
var strRange=document.selection.createRange();
var strChar=objList.options[intSelected].value;
strRange.text=strChar;
}else{
objList.form.theLoca.value+=objList.options[intSelected].value;
}
objList.selectedIndex=0;
theLoca.focus();
}
}

and here’s the listbox:

<select class=”char” tabindex=”-1″ onChange=”insertChar(this, ‘thearea’)”>
<option selected value=””>Symbols</option>
<option value=”&reg;”>&reg; R Mark</option>
<option value=”&copy;”>&copy; Copyright</option>
<option value=”&trade;”>&trade; Trademark</option>
</select>

thearea is the name of the textfield
what is causing it to not work?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@tabzterSep 07.2005 — You cant access a variable text box with [B]objList.form.theLoca.value[/B] even if you use [B]insertChar(this, 'thearea')[/B] with theLoca='thearea'. The scripting engine thinks that [I]theLoca[/I] is an html form element called theLoca that you are trying to access.

I decided to re-write and cut down your above code without error checking for IE/Mac (which you can add yourself). Let me know if it works...

<i>
</i>&lt;script type="text/javascript"&gt;
function insertChar(objList,theLoca)
{
eval("window.myForm."+theLoca+".value+=objList.options[objList.selectedIndex].value;")
theLoca.focus();
}
&lt;/script&gt;
.............
&lt;form name="myForm"&gt;
&lt;select class="char" tabindex="-1" onChange="insertChar(this,'text1')"&gt;
&lt;option selected value=""&gt;Symbols&lt;/option&gt;
&lt;option value="&amp;reg;"&gt;&amp;reg; R Mark&lt;/option&gt;
&lt;option value="&amp;copy;"&gt;&amp;copy; Copyright&lt;/option&gt;
&lt;option value="&amp;trade;"&gt;&amp;trade; Trademark&lt;/option&gt;
&lt;/select&gt;
&lt;input type="text" name="text1" id="text1" /&gt;
&lt;input type="text" name="text2" id="text2" /&gt;
&lt;/form&gt;
.........


notice that I have used eval to ensure:

1) older browser compatibility.

2) cross browser compatibility.

3) dynamic naming can be used.
×

Success!

Help @jasolio44 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.28,
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,
)...