Welcome to Dream.In.Code
Become an Expert!

Join 149,504 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,369 people online right now. Registration is fast and FREE... Join Now!




Set The Value in a Drop-Down via JS

 
Reply to this topicStart new topic

Set The Value in a Drop-Down via JS

FernFerret
20 Jul, 2007 - 01:58 PM
Post #1

New D.I.C Head
*

Joined: 14 Apr, 2005
Posts: 25


My Contributions
Hello everyone, its been a while, but alas I am back, this time in the javascript section.

I just want to make sure I am doing everything right I keep getting tons of errors and I think it stems from how I set my drop-down menu.

The Page posts to itself with form number 3, so if certain conditions are met, the drop-down box needs to be set to "Gift Card" which is one of the options.

currently I am saying :

CODE
document.add_sale.paid_with.value = "Gift Card";

but that isnt really working at all, the error i'm getting on the page is:
Error: 'document.add_sale.paid_with' is null or not an object.

The form name is add_sale
The Dropdown name is paid_with

at one time i was using
CODE
document.add_sale.paid_with.option.value = "Gift Card";
but i dont think that was working either, much help would be appreciated!

Thanks,
Eric
User is offlineProfile CardPM
+Quote Post

ahmad_511
RE: Set The Value In A Drop-Down Via JS
21 Jul, 2007 - 12:29 PM
Post #2

D.I.C Regular
Group Icon

Joined: 28 Apr, 2007
Posts: 351



Thanked: 8 times
Dream Kudos: 400
My Contributions
HI
TRY TO DO THIS
CODE

<option value="value1">my first chioce</option>


Now when you want to change the option's value do this
CODE

document.add_sale.paid_with.value = "value1";


see... that's what the value refer to, (the option's value property) not the text within the option tag

If i missed somthing don't hesitate to reply with more details
Best Regards
User is offlineProfile CardPM
+Quote Post

FernFerret
RE: Set The Value In A Drop-Down Via JS
23 Jul, 2007 - 11:25 AM
Post #3

New D.I.C Head
*

Joined: 14 Apr, 2005
Posts: 25


My Contributions
Thank you for your help, but I still have a problem. I get the following error when I call that code from a function (not when i insert it in for example the onLoad page):
Line: 32
Char: 3
Error: 'document.add_sale.paid_with' is null or not an object
Code: 0

Any thoughts?
THis only happens when I call it from a function...

Thanks,
Eric



User is offlineProfile CardPM
+Quote Post

Arbitrator
RE: Set The Value In A Drop-Down Via JS
25 Jul, 2007 - 08:16 AM
Post #4

D.I.C Regular
Group Icon

Joined: 26 Jan, 2005
Posts: 492



Thanked: 1 times
My Contributions
QUOTE(FernFerret @ 23 Jul, 2007 - 02:25 PM) *

Thank you for your help, but I still have a problem. I get the following error when I call that code from a function (not when i insert it in for example the onLoad page):
Line: 32
Char: 3
Error: 'document.add_sale.paid_with' is null or not an object
Code: 0

Any thoughts?
THis only happens when I call it from a function...
It would really help us help you if you would show your code by either posting it or by (preferably) linking to a live copy. With that said, all I can say is to make sure that the element exists and that there are no typographical errors. This includes making sure that the element exists at the time that the function is executed; attempting to reference an element before the part of the document containing it is downloaded is a common error.

I should point out that (A) you should be using DOM methods such as document.getElementsByName("paid_with")[0] or, better yet, document.getElementById("paid_with") and (B) the name attribute of the form element was deprecated in XHTML 1.0. For the latter, use id attributes instead name.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:44PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month