Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
get value of drop down
Old 11-08-2009, 02:37 PM get value of drop down
Extreme Talker

Posts: 176
Trades: 0
I just need to set the value of the currently selected option of a dropdown box.

HTML Code:
<html>
<body>

<script type="text/javascript">
function getRare()
{alert(document.myForm.rarity.options[rarity.selectedIndex].value);
}
</script>

<form name="myForm">
<select name="Rarity">
    <option value="0">Common</option>
    <option value="1">Uncommon</option>
    <option value="2">Rare</option>
    <option value="3">Other</option>
</select>
<input type="button" value="pick" onclick="getRare();" />
</form>

</body>
</html>
can you tell me what's wrong?
Skeddles is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-08-2009, 02:59 PM Re: get value of drop down
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
No idea.
What does it do, or not?
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-08-2009, 03:29 PM Re: get value of drop down
Extreme Talker

Posts: 176
Trades: 0
An alert should pop up saying either 0 1 2 or 3, but nothing happens...

This isn't my actual code, I'm trying to do some ajax, but I cut out all the other stuff to make it easier.
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 11-08-2009, 03:34 PM Re: get value of drop down
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Why not just use the onchange event of the select element?
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-08-2009, 03:39 PM Re: get value of drop down
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
2 things are wrong:
1) Javascript is case sensitive. If your <select> is named Rarity, form.rarity is not the same.
2) When you look up the selectedIndex value, you have to give the full path to it.
Code:
document.myForm.rarity.options[rarity.selectedIndex]
should be
Code:
document.myForm.rarity.options[document.myForm.rarity.selectedIndex]
HTML Code:
<html>
<body>

<script type="text/javascript">
function getRare(){
  frm=document.myForm
  val=frm.Rarity.options[frm.Rarity.selectedIndex].value
  alert(val);
}
</script>

<form name="myForm">
<select name="Rarity">
    <option value="0">Common</option>
    <option value="1">Uncommon</option>
    <option value="2">Rare</option>
    <option value="3">Other</option>
</select>
<input type="button" value="pick" onclick="getRare();" />
</form>

</body>
</html>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-08-2009, 03:43 PM Re: get value of drop down
Extreme Talker

Posts: 176
Trades: 0
because what the ajax is doing is submitting all the form data and adding a row to a database with php, so it has to be done all at once, and I dunno how to do post with ajax, so I'm just doing

Code:
varNumber = document.myForm.number.value;
varName = document.myForm.name.value;
xmlhttp.open("GET","addcard.php?number="+varNumber+"&name="+varName,true);
xmlhttp.send(null);
which works fine for textboxes, but not with dropdowns

EDIT: thanks tripy
I always forget about case sensitive ><

Last edited by Skeddles; 11-08-2009 at 03:47 PM..
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 11-08-2009, 04:23 PM Re: get value of drop down
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
POSTing with AJAX is similar to get, you have to send the parameters length as well

http://www.openjs.com/articles/ajax_...using_post.php
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-09-2009, 08:13 PM Re: get value of drop down
Extreme Talker

Posts: 176
Trades: 0
I'll check it out, but at the moment it isn't needed, considering I'm just doing short text fields.


And if I wanted to do another ajax thing on the same page, should I make a whole other ajax function, or make that one work for both(with if statements?)?

And how should I go about retrieving multiple pieces of data? Like I want to make another for editing, so I'd have to load each thing and then put them into the form, but loading it 4 times seems ridiculous.
Skeddles is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to get value of drop down
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26537 seconds with 12 queries