?
Solved

setting onchange event function parameters dynamically

Posted on 2003-03-12
5
Medium Priority
?
789 Views
Last Modified: 2007-12-19
Hi,

I'm trying to do the following, when creating a new combo box and adding it to the page:

//
// create the new combo box
//
var cbo = document.createElement("select");
cbo.setAttribute('id', cboID);
cbo.onchange = function(){DisplayNextOptions(intNextNum, document.questions.elements[cboID].options.selectedIndex.value)};
cbo.className = 'combo';


- where 'questions' is the name of my form and cboID is a variable containing the name of the combo box we're adding to the page.

If I add this to the HTML (not dynamically) it works fine:

this.options[selectedIndex].value

What syntax should I use when I want to return the value attribute of a dynamically created combo box?

Thanks,
Col
0
Comment
Question by:montyburns
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 14

Expert Comment

by:avner
ID: 8118666
What do you mean by : " to return the value attribute of a dynamically created combo box"

Return it to combo as what ? as a new added option ? as an option to select ?

0
 

Author Comment

by:montyburns
ID: 8118719
sorry, I was trying to be brief and cut out as much superflous info as I could.

My app uses multiple dependent combo boxes to drill down into info, sort of like a tree-view. Each time you select an option in a combo box, I want the value attribute to be sent to the JS function DisplayNextOptions. This function takes the value of the selected option, queries a SQL DB (using XMLHTTP) and then builds the next combo box in JS before adding it to the document.

My first combo box is generated by ASP and so the onchange event is simply:

onChange='DisplayNextOptions(1, this.options[selectedIndex].value)'

- which works fine.

However if I try to add this same code to a combo created using JS, it doesn't work.  I've tried loads of different syntax to no avail.

Does this make sense?

Thanks,
Col
0
 
LVL 11

Accepted Solution

by:
jausions earned 180 total points
ID: 8118729
Hi,

Try this, you're mixing the .options, .selectedIndex and .value properties;


   cbo.onchange = function(){DisplayNextOptions(intNextNum, document.questions.elements[cboID].options[document.questions.elements[cboID].selectedIndex].value)};


Best regards,

 -Philippe
0
 

Author Comment

by:montyburns
ID: 8118739
sorry, I was trying to be brief and cut out as much superflous info as I could.

My app uses multiple dependent combo boxes to drill down into info, sort of like a tree-view. Each time you select an option in a combo box, I want the value attribute to be sent to the JS function DisplayNextOptions. This function takes the value of the selected option, queries a SQL DB (using XMLHTTP) and then builds the next combo box in JS before adding it to the document.

My first combo box is generated by ASP and so the onchange event is simply:

onChange='DisplayNextOptions(1, this.options[selectedIndex].value)'

- which works fine.

However if I try to add this same code to a combo created using JS, it doesn't work.  I've tried loads of different syntax to no avail.

Does this make sense?

Thanks,
Col
0
 

Author Comment

by:montyburns
ID: 8118750
thanks Phillipe - easy when you know how, eh?!

I'd give you more points if I had them to give!

Col

ps - sorry about the repeated post, I refreshed the page by mistake
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question