Welcome to WebmasterWorld Guest from 54.211.120.181

Forum Moderators: open

Message Too Old, No Replies

function in onchange event

function in onchange event

     
5:11 pm on Oct 14, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 8, 2004
posts: 59
votes: 0


Hi,
I'm having some difficulty in accessing an 'external' function from my onchange event. It will work when the function is included 'inline,' but it's not working when the function is 'stand alone.'
Additionally, when it works, the variable isn't being passed - the alert I use says "[object Event]".
Thanks for any help - much appreciated.


<head>
function nameTextBox2(selectBoxName)
{
alert("from the function..."+selectBoxName);
}

function add()
{
var foo = document.getElementById('fooBar');

//Creating a Select Box
var selectBox = document.createElement("Select");
var selectBoxName="strObjDesc";
selectBox.name=selectBoxName;
selectBox.id=selectBoxName;
//below line works but alert doesn't pick up variable
selectBox.onchange=function nameTextBox2(selectBoxName) {alert("from the function..."+selectBoxName)}
//below line (2nd version of above) doesn't work
// selectBox.onchange=nameTextBox2(selectBoxName)

//Creating first Option
var charCodeRange = {
start: 65,
end: 90
}
for (var cc = charCodeRange.start; cc <= charCodeRange.end; cc++)
{
var option1 = document.createElement("OPTION");
var option
//alert(cc);
option1.text=String.fromCharCode(cc);
option1.text=option1.text.toLowerCase(cc);
option1.value=String.fromCharCode(cc);
option1.value=option1.value.toLowerCase(cc);
selectBox.options.add(option1);
}

foo.appendChild(selectBox);
}

</script>
</head>
<body>
<form>
<INPUT type="button" value="Add" onclick="add()"/><br />
<div id="fooBar">&nbsp;</div>
</form>
</body>

5:38 pm on Oct 14, 2010 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:4988
votes: 12



selectBox.onchange=function nameTextBox2(selectBoxName) {alert("from the function..."+selectBoxName)}

Try removing the "nameTextBox2".


selectBox.onchange = function (selectBoxName) {
alert("from the function..."+selectBoxName);
}
5:57 pm on Oct 14, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 8, 2004
posts: 59
votes: 0


Thanks for your reply, but after removing the function name in the 'inline' version, it still is doing the same thing.

(in FF: "[object Event]" and in IE8: "undefined")


Is there a way to make the 'stand alone' version of the function work, as well?
8:33 pm on Oct 14, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Feb 11, 2006
posts:481
votes: 0


Hi,

Try:
selectBox.onchange=function(){nameTextBox2(selectBoxName);}


Andrew
9:44 pm on Oct 14, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 8, 2004
posts: 59
votes: 0


Thanks for the reply. This works really well!
Would the inline version be similar?
10:03 pm on Oct 14, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Feb 11, 2006
posts:481
votes: 0


Hi,

Instead of calling nameTextBox2 you would put it's body directly into the anonymous function.

Andrew
2:42 pm on Oct 15, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 8, 2004
posts: 59
votes: 0


Many thanks!