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
Why does this Javascript DOM example from w3schools not work?
Old 06-28-2007, 04:49 PM Why does this Javascript DOM example from w3schools not work?
Experienced Talker

Posts: 33
Trades: 0
Well I use www.w3schools.com quite a lot for coding examples. The're normally fully working.

This particular example works on thieir site, but not when I copy and paste the code into a new .html doc. All I get is ONE outputed value; Apple and I should get all four as I do on their website.

The URL is: http://www.w3schools.com/js/tryit.as...select_options

Any ideas?

Here's the source code:

Code:
<html>
<head>
<script type="text/javascript">
function getOptions()
  {
  var x=document.getElementById("mySelect");
  for (i=0;i<x.length;i++)
    {
    document.write(x.options[i].text)
    document.write("<br />")
    }
  }
</script>
</head>
<body>
<form>
Select your favorite fruit:
<select id="mySelect">
  <option>Apple</option>
  <option>Orange</option>
  <option>Pineapple</option>
  <option>Banana</option>
</select>
<br /><br />
<input type="button" onclick="getOptions()" value="Output all options">
</form>
</body>
</html>
monkey64 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-28-2007, 09:48 PM Re: Why does this Javascript DOM example from w3schools not work?
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
At first glance I would guess that
Code:
for (i=0;i<x.length;i++)
should be
Code:
for (i=0;i<x.options.length;i++)
But, I haven't tested.

hope it helps (and works)
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 06-30-2007, 08:36 AM Re: Why does this Javascript DOM example from w3schools not work?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
What browser are you testing in?

only FF & Opera gave all four option values for me.

IE6, NS7.1 and Moz1.7 only returned the first one.
__________________
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 06-30-2007, 03:11 PM Code only works in Firefox, not IE!!!
Experienced Talker

Posts: 33
Trades: 0
You're correct!

The code works fine in Firefox and I get four values returned.

Using Internet Explorer [tested 5, 5.5,6 and 7], you get one value returned.
Now I'm really confused!

I thought Javascript was universally accepted by all browsers. Am I wrong in this assumption?
monkey64 is offline
Reply With Quote
View Public Profile
 
Old 06-30-2007, 07:18 PM Re: Why does this Javascript DOM example from w3schools not work?
Novice Talker

Posts: 12
Location: Somewhere on a Planet.
Trades: 0
I know that I had to download a new browser when I started to work with JavaScript, cause IE didn't like it. I think there is an option to allow JS to work on IE, but am not sure where it is.
NinjaKitty is offline
Reply With Quote
View Public Profile
 
Old 06-30-2007, 09:27 PM Re: Why does this Javascript DOM example from w3schools not work?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
No, javascript is available on IE as almost any other browser.

The problem is that javascript is implemented at client side (in the browser).
So, different browser may implement javascript differently, and they may be bugs (or "features" as some would say) in those implementations that can make them react differently to the same command.

They can even be totally different command to do the same thing.

To make it short, Javascript can be a real hell...
But I like it !
__________________
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 06-30-2007, 09:41 PM Re: Why does this Javascript DOM example from w3schools not work?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
And for the curiosity, it looks like on IE, the document.write, once called, place the element where it writes in "read-only" mode.

This correct the statement, but using document.write is really to avoid.

Use the DOM instead, and create/append elements in the page.
http://developer.mozilla.org/en/docs....createElement
The exemple above works everywhere (As far as I know) and is the way you should take.

Code:
function getOptions(){
  var x=document.getElementById("mySelect");
  var str="";
  for (var i=0;i<x.options.length;i++){
    str+=x.options[i].text+"<br/>";
  }
  document.write(str);
}
__________________
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!
 
Reply     « Reply to Why does this Javascript DOM example from w3schools not work?
 

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.35734 seconds with 12 queries