Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Onchange event for a dropdown

    Hi friends,

    I have a drop down list in html which contains 3 items; Black, white,blue..
    If i select "Black", a pop up window should come up. (Alert or confirm);

    Please help me out; i am new to JS and need to complete My academic project work.. Please help me in learning..Thanks in advance.

    <select id="mylist">
    <option value="White">White</option>
    <option value="White">Black</option>
    <option value="White">blue</option>
    </select>

    Please edit the code so that if "Black" is selected, pop up window should come up

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,930
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Here you are:-

    Code:
    <select id="mylist" onchange = "go()">
    <option value="">Select a colour</option>
    <option value="White">White</option>
    <option value="Black">Black</option>
    <option value="Blue">Blue</option>
    </select>
    
    <script type = "text/javascript">
    function go() {
    var x = document.getElementById("mylist").value;
    if (x == "Black") {
    alert ("You selected BLACK");
    var ans = confirm ("Do you want to do something now?");
    if (ans) {
    alert ("OK! I'll do it right now!");
    }
    else {
    alert ("What a pity!");
    }
    }
    }
    
    </script>

    BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's voluntary unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.




    Quizmaster: Complete the name of the Shakespeare play "Troilus and ....."?
    Contestant: Er... John?

  • Users who have thanked Philip M for this post:

    scrappy (06-16-2009)

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's voluntary unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.
    It doesn't give me that impression. It gives the impression that you have been thanked just for reading the post, and most likely get more thanks after trying to answer it.

    Code:
    var x = document.getElementById("mylist").value;
    That is really simple. I've always seen:

    Code:
    var x = document.getElementById("mylist").options[document.getElementById("mylist").options.selectedIndex].value;
    and have used it out of habit.
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog

  • #4
    New Coder
    Join Date
    Nov 2008
    Posts
    59
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Just what I was looking for, thanks Philip! Saves me posting the question

    (I actually ended up here whilst looking at the "Post a javascript" forum looking for the exact same thing and noticed this moved post)


  •  

    Tags for this Thread

    Posting Permissions

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