coolumanga
11-10-2010, 06:43 AM
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
Philip M
11-10-2010, 07:24 AM
Here you are:-
<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)
coolumanga
11-10-2010, 07:53 AM
Here you are:-
<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
Philip M
11-10-2010, 08:05 AM
The fault, dear Brutus, lies not in the stars but .......... :rolleyes:
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?
Logic Ali
11-10-2010, 03:42 PM
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.
<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>