SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast Igloo's Avatar
    Join Date
    Dec 2002
    Location
    Australia
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Two Javascript Enquiries

    Hello,

    I was wondering whether a few things could be achieved using Javascript, and how I would go about implementing them into my project.

    Firstly, is their an emulation of PHP's foreach() in Javascript? And how would I go about using it, with both normal and associative arrays (if it is different from PHP's method)?

    Secondly, when working with <select> menus how would I go about manipulating the <option></option> tags within? As in, adding / removing / modifying the value and title within an <option> tag, where '<option value="value">title</option>'.

    Thankyou for your assistance,

    Igloo...

  2. #2
    SitePoint Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Firstly the array part:

    It's just "for (foo in thearray)" syntax:

    Code:
    <html><body><script>
    <!--
    
    myarray = ["blah","foo","bar"];
    
    for (o in myarray){
    	document.write(o + " = " +myarray[o]+"<br />");
    }
    
    var my_cars= new Array()
    my_cars["cool"]="Mustang";
    my_cars["family"]="Station Wagon";
    my_cars["big"]="SUV";
    
    for (o in my_cars){
    	document.write(o + " = " +my_cars[o]+"<br />");
    }
    
    //-->
    </script></body></html>

  3. #3
    SitePoint Enthusiast Igloo's Avatar
    Join Date
    Dec 2002
    Location
    Australia
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, thankyou very much for that.

    Is there anyone that can help with the second part of my enquiry?

    Ta,

    Igloo...

  4. #4
    SitePoint Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Sorry - I got distracted before I replied to the second.

    There are so many scripts around to do this - here's the first one I found with google: http://www.mattkruse.com/javascript/...ox/source.html

    One thing that most people seem to forget (or never knew) is that you need to refresh the page when you dynamically add an option in Netscape 4. See this from the N4 documentation:

    you can create new options using the Option constructor and add those to a selection list. After you create an option and add it to the Select object, you must refresh the document by using history.go(0). This statement must be last. When the document reloads, variables are lost if not saved in cookies or form element values.
    piglet

    Moderator at The City

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Piglet
    One thing that most people seem to forget (or never knew)
    For me, you can file that under "never knew", or "never cared". As far as NS4 goes, the two are basically interchangeable
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    SitePoint Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi beetle,

    lol - I know your feelings on N4...understand them even....but there are still people out there using it (in fact it's still the only browser installed in one build of my company's desktop which is only now being updated to an IE 6-containing build)

    I guess scripts should attempt compatibilty when this is possible or if the client needs it, and a if (document.layers)history.go(0); to do it isn't too much to ask...is it?
    piglet

    Moderator at The City

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, it's not. Honestly, though, 95% of the scripts I write reject NS4 with the first line.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •