Can someone please help me find if there is any option selected I currently have this
var Selected = $$('myslected_id').getElements('[selected]') ;
if(Selected==null){
$('selectedresult').set('text','Nothing Selected');
}else{
$('selectedresult').set('text','Something Selected');
}
<div id="selectedresult"></div>
<select id="myslected_id" name="myslected_name" multiple="multiple">
<optgroup label="mylabel">
<option value="1">Value1</option>
<option value="2" selected="selected">Value1</option>
<option value="3">Value2</option>
</optgroup>
</select>
current form select should output Something Selected
Thank you!
i think you'd be better off using the getSelected method. it gets... the selected stuff. :)
first of all you misspelled 'myselected_id' on your <select>
<select id="myselected_id" name="myselected_name" multiple="multiple">
<optgroup label="mylabel">
<option value="1">Value1</option>
<option value="2" selected="selected">Value1</option>
<option value="3">Value2</option>
</optgroup>
</select>
mootools section
var Selected = $$('#myselected_id option[selected]');
if (Selected.length == 0) {
$('selectedresult').set('html', 'nothing selected');
} else {
$('selectedresult').set('html', 'Something Selected');
}
Here is the Jsfiddle This will grab all options elements that are selected. Your understanding of usage for $$ isn't correct. Please read this section. Also, an empty array does not equal to null in javascript.
Thank you kyjy112 ! I fund another way also but yours is shorter yes , im still mixing php array with js , beginer , this is what i came up with var
MySelection = $('myselectid');
MySelection.addEvent('domready', function(){//domready to use on load
if(MySelection.getElement("[selected]")) {
//$("oresultdiv").set("text", MySelection.getElement(":selected").text)
$("resultdiv").set("text", "Something")
}else {
$("resultdiv").set("text", "nothing")
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.