PDA

View Full Version : Affect new value to combox which is not in options



sfchun
02-20-2013, 10:11 AM
Hello

I wonder if there is a way to affect a value to a combobox which does not exists in predefined options ?

html part

<select name="EmailList" id="EmailListID">
<option value=""></option>
<option value="a@b.com">a@b.com</option>
<option value="c@d.com">c@d.com</option>
</select>
<textarea style="resize:none;overflow:hidden;display:none;" rows="1" cols="32" maxlength="128" name ="otherEmail" id="otherEmailID" value=""></textarea>
<input type="checkbox" name="EmailCheckBox" id="EmailCheckBoxID" onChange="checkEmailCheckBox();">


javascript part


function checkEmailCheckBox(){

var EmailListValue = document.getElementById('EmailListID').value;
var OtherEmailValue = document.getElementById('EmailListID').value;

if (document.getElementById('EmailCheckBoxID').checked){
document.getElementById('otherEmailID').value = EmailListValue; // <---- always works
document.getElementById('EmailListID').style.display = 'none';
document.getElementById('otherEmailID').style.display = 'block';
} else {
document.getElementById('EmailListID').value = OtherEmailValue; // <---- works only if the set value is present in combobox options ( here : a@b.com or c@d.com )
// if try to force another value it becomes 'empty value'...
document.getElementById('EmailListID').style.display = 'block';
document.getElementById('otherEmailID').style.display = 'none';
}
}


Thanks

sfchun
03-06-2013, 12:50 PM
it seems I found a new thing we cannot do with javascript (http://www.dynamicdrive.com/forums/showthread.php?21964-What-you-CANNOT-do-with-JavaScript) :p

jscheuer1
03-06-2013, 01:04 PM
If you want to set a select's value to something none of its options have, you need to create a new option element with that value and append it to the select element first.

Demo:

http://home.comcast.net/~jscheuer1/side/tidbits/options-email-h.htm

sfchun
03-07-2013, 01:45 AM
Thanks a lot for your reply !