homepage Welcome to WebmasterWorld Guest from 54.196.196.62
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
function in onchange event
function in onchange event
buck1107




msg:4216793
 5:11 pm on Oct 14, 2010 (gmt 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>


 

Fotiman




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


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

Try removing the "nameTextBox2".


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

buck1107




msg:4216817
 5:57 pm on Oct 14, 2010 (gmt 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?

Little_G




msg:4216888
 8:33 pm on Oct 14, 2010 (gmt 0)

Hi,

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

Andrew

buck1107




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

Thanks for the reply. This works really well!
Would the inline version be similar?

Little_G




msg:4216933
 10:03 pm on Oct 14, 2010 (gmt 0)

Hi,

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

Andrew

buck1107




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

Many thanks!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved