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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdownlist - display different record

Status
Not open for further replies.

olypost

Programmer
Apr 15, 2005
43
US
I have a client-side button that I want to call a
javascript function via onclick.

All I need to do is reset a dropdownlist back to the first
record in the list.

Tried: document.forms['myform'].elements['mydropdownlist'].selectedIndex = 0;

and tried:

document.getElementById("mydropdownlist").selectedIndex = 0

Both cases give me errors at runtime.

What am I missing?

Thanks
 
>Both cases give me errors at runtime.
>What am I missing?
The error is not coming from the .selectedIndex line.
 
If I remove that line, no errors.

When I enter: document.getElementById("mydropdownlist").
for some reason it doesn't have selectedIndex in the pick list.

I can do:
document.getElementById("mydropdownlist").innertext = "";

without the runtime error, but this isn't what I want.

Don't know why selectedIndex doesn't appear in the list
(innerhtml, innertext, etc are in the list)
 
What do you get this?
[tt] alert(document.getElementById("mydropdownlist").type)[/tt]

 
Either you have some inline script before the select-one element is rendered or some iframe or else. Is it an inline script? Do you want the forum to guess riddle?
 
It is all client-side in the .aspx file.

<script lang=Javascript>
function clearfields ()
{
document.getElementById("mydropdownlist").selectedIndex = 0;

}
</script>

I don't want to go to the server to do this. Is this possible?
 
How is it called? How is the form look like? Of course it is possible. It is read-write property, the selectedIndex. That's about it.
 
I call it in an html button.

..... type=button onclick="clearfields();"....

Then I just call the javascript function. I am confused why I can't even put in the .selectedIndex
 
use getElementById for id, use getElementsByName with index for name or use formreference.elements[name] to reference the select element, if you test your page on moz. Otherwise, if you do not feel comfortable to pose code here, bring your question to asp.net forum.
The question does not deserve exchanges for more than 7 rounds.
 
I solved it.

document.getElementById("mydropdownlist").value = 0;

Strange-- .value isn't even in the list of choices in Visual Studio

But it works beautifully now. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top