SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast Nick Karpov's Avatar
    Join Date
    Jan 2007
    Location
    Toronto Ontario
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Little help with dropdown control (Ajax)

    Hey all,

    I've been looking through many AJAX tutorials but I just keep getting lost, they are all written by giving the final source code, and don't really help me.

    What I want to do is VERY elementary, and I was wondering if the kind community of sitepoint would be willing to help me out a bit.

    I have a dropdown box with any number of selections (lets say 3 to keep it simple) and depending on which one of those selections is made, an image loads beneath the dropdown box.

    I don't really have any previous experience with Javascript so this will be my first time, any and all help is appreciated.

    thanks a lot!

    nick

  2. #2
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll start you off and I'll let someone else carry on from me!

    Code:
    var pElement = document.getElementById('mySelect');
    var pSelected = pElement.childNodes[pElement.selectedIndex];
    That will give you the selected index from your select field (mySelect). Then you can get the data from that like so:

    Code:
    szValue = pSelected.getAttribute('value');
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  3. #3
    SitePoint Enthusiast Nick Karpov's Avatar
    Join Date
    Jan 2007
    Location
    Toronto Ontario
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot thats got me started! I'm testing it out and finding something strange, I've made it so every time you switch your selection it should create a pop up box and say what your selection is, except it doesn't work ?_?

    The Javascript is simple enough, and so is the HTML.. why isn't it working?

    Code JavaScript:
    function OnChange ()
    {
    	var pElement = document.getElementById('select1');
    	var pSelected = pElement.childNodes[pElement.selectedIndex];
    	var szValue = pSelected.getAttribute('value');
     
    	alert(szValue);
     
        return true;
    }

    Code HTML4Strict:
    <select id="select1" onchange='OnChange();'>
    <option value="0">Please select your preffered time slot</option>
    <option value="1">First Time Slot</option>
    <option value="2">Second Time Slot</option>
    <option value="3">Third Time Slot</option>
    </select>

    For some strange reason, when i select First Time Slot it gives me a value of 0 in the pop up box, nothing if i select the second time slot and a "1" if i select the Third time slot... what is going here? i'm so confused


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
  •