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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    122
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Combobox select value alert message

    hey

    i have combo box

    it has names

    name -1
    name-2
    name-3
    name-4

    when select a name-1 just display a alert message of age

    plz help me to do this

    plz any sample coding for this

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Here you are:-

    Code:
    <form name = "myform">
    <select list name = "people" onchange = "showAge()">
    <option value = "" age = "" >Select A Name</option>
    <option value = "Peter" age = "19" >Peter</option>
    <option value = "Susan" age = "22" >Susan</option>
    <option value = "Michael" age = "30" >Michael</option>
    <option value = "Jenny" age = "24" >Jenny</option>
    </select>
    </form>
    
    <script type = "text/javascript">
    function showAge() {
    var n = document.myform.people.value;
    if (n != "") {
    var a = document.myform.people.options[document.myform.people.selectedIndex].age;
    alert (n + " is aged " + a)
    }
    }
    </script>
    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Posts
    122
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Here you are:-

    Code:
    <form name = "myform">
    <select list name = "people" onchange = "showAge()">
    <option value = "" age = "" >Select A Name</option>
    <option value = "Peter" age = "19" >Peter</option>
    <option value = "Susan" age = "22" >Susan</option>
    <option value = "Michael" age = "30" >Michael</option>
    <option value = "Jenny" age = "24" >Jenny</option>
    </select>
    </form>
    
    <script type = "text/javascript">
    function showAge() {
    var n = document.myform.people.value;
    if (n != "") {
    var a = document.myform.people.options[document.myform.people.selectedIndex].age;
    alert (n + " is aged " + a)
    }
    }
    </script>
    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)
    thankz a lot for reply and it worked

    but ther is small error

    when i seleted a value it will give

    age is undifined

    so how can i define a age

    plzz solve this

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    The fault, dear Brutus, lies not in the stars but ..........

    There is no error in the code. You have messed it up in some way.

    The ages are defined in the option list:

    <option value = "Peter" age = "19" >Peter</option>


    OK?

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by coolumanga View Post
    hey

    i have combo box

    it has names

    name -1
    name-2
    name-3
    name-4

    when select a name-1 just display a alert message of age
    If your page has to validate, you cannot use unofficial attributes to store data. Here are two ways of doing it depending upon what the value attribute needs to contain.
    Code:
    <form action='#'>
    <p>
    <select name = "people" onchange = "showAge(this)">
    <option value = "">Select A Name</option>
    <option value = "Peter|19">Peter</option>
    <option value = "Susan|22">Susan</option>
    <option value = "Michael|30">Michael</option>
    <option value = "Jenny|24">Jenny</option>
    </select>
    </form>
    
    <script type = "text/javascript">
    
    function showAge( box ) 
    {
     var n = box.value.split('|');
     
     if( n[ 1 ] ) 
      alert( n[ 0 ] + " is aged " + n[ 1 ] );
    }
    
    </script>
    
    <form action='#'>
    <p>
    <select name = "people" onchange = "showAge(this)">
    <option value = "">Select A Name</option>
    <option value = "19">Peter</option>
    <option value = "22">Susan</option>
    <option value = "30">Michael</option>
    <option value = "24">Jenny</option>
    </select>
    </form>
    
    <script type = "text/javascript">
    
    function showAge( box ) 
    {
     var idx = box.selectedIndex
     
     if( idx > 0 )
      alert( box.options[ idx ].text + ' is ' + box.value ); 
    }
    
    </script>


  •  

    Posting Permissions

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