Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get selectedIndex

Status
Not open for further replies.

copeZero

Programmer
Aug 31, 2007
46
0
0
CA
Hi, this JS issue is driving me crazy, any reasons why i can't get the select index value of a dropDown:

dobMonth.Attributes.Add("onchange", "alert(document.forms[0].dobYear.value);alert(this.value);alert(document.getElementById('dobDay').options[document.getElementById('dobDay').selectedIndex].value);");

I get the first alert, the second alert, but not the third alert ...?

Besides:
document.getElementById('dobDay').options[document.getElementById('dobDay').selectedIndex].value)

I've tried by associative array:
document.forms[0].dobDay.options[document.forms[0].dobDay.selectedIndex].value

I know the JS is good, so it must be a flaw in my thinking.. that i can access the value as such. How can this be accomplished?

Thanks
 
Here's the place to ask: forum216

Good Luck,

Alex

[small]----signature below----[/small]
Now you can go where the people are one!
Now you can go where they get things done!
 
copeZero - I may understand your question now. Is 'dobDay' a server-side ASP.net control?

If so, you'll need to expose its' selected index as a property, like this:

Code:
    public int selInd
    {
        get
        {
            return dobDay.SelectedIndex;
        }
    }

Then, you access it from client side script like this:

Code:
    function myAlert(){
    alert(<%=selInd %>);
    }

The <% %> indicates that you are getting the value from the server side when the page loads. For this to respond to change, auto-post back needs to be enabled on your drop-down.

The ASP.net guys might no of a better way to do this.

Hope this helps,

Alex

[small]----signature below----[/small]
Now you can go where the people are one!
Now you can go where they get things done!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top