Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 805
  • Last Modified:

setting onchange event function parameters dynamically

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
montyburns
Asked:
montyburns
  • 3
1 Solution
 
avnerCommented:
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
 
montyburnsAuthor Commented:
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
 
jausionsCommented:
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
 
montyburnsAuthor Commented:
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
 
montyburnsAuthor Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now