I have to deal with some old Javascript code that is throwing an error at addOption and selectOptions
Error: Object has no method selectOptions
Can someone explain me why is it not working? I am using jQuery 1.3
$("some_id").addOption(nodeId, nodeName); // add to list
$("some_id").selectOptions(/^~~/i, true); // clear selection
I solved the addOption line by this
$("some_id")[0].options.add( new Option(nodeName,nodeId));
but I'm still stuck with selectOptions error.
UPDATE just found out the application is also using Dojo. Could that be the problem? Are these methods Dojo specific?
Thanks!
I have to deal with some old Javascript code that is throwing an error at addOption and selectOptions
Error: Object has no method selectOptions
Can someone explain me why is it not working? I am using jQuery 1.3
$("some_id").addOption(nodeId, nodeName); // add to list
$("some_id").selectOptions(/^~~/i, true); // clear selection
I solved the addOption line by this
$("some_id")[0].options.add( new Option(nodeName,nodeId));
but I'm still stuck with selectOptions error.
UPDATE just found out the application is also using Dojo. Could that be the problem? Are these methods Dojo specific?
Thanks!
Share Improve this question edited Oct 14, 2013 at 11:01 Jabran Saeed asked Oct 14, 2013 at 10:38 Jabran SaeedJabran Saeed 6,1984 gold badges23 silver badges38 bronze badges 3-
Perhaps
first_option = $('some_id')[0].options[0].value; $('some_id').val(first_option);
– Mina Commented Oct 14, 2013 at 10:54 -
Or even
$('select').val('')
– Mina Commented Oct 14, 2013 at 10:55 - have you checked if there's an selectOption function declaration in your code before? The error says that the function doesn't exist an as far as I know there is no native jQuery selectOptions function. – damian Commented Oct 14, 2013 at 10:57
3 Answers
Reset to default 2use Jquery Append to add options like this
$("yourid/class here").append($("<option></option>").attr("value", youroption-value).text(youroption-text));
try this, you can write your own methods:
$.fn.addOption = function(optText, optValue){
var option = new Option(optText, optValue);
return this.append(option);
};
$.fn.selectOption = function(toSelect){
var $option = this.find("option[value='"+toSelect+"']");
if($option.length > 0){
//if option with the value passed on found then select it
$option.prop("selected","selected");
}else{
alert("option not found");
}
};
var $select = $("#selectOption");
$select.addOption("Dummy1",2);
$select.addOption("Dummy2",3);
$select.selectOption(231);
working fiddle here: http://jsfiddle/maverickosama92/rGzPS/1/
Finally found whats wrong with it. These methods e from a jquery plugin by TexoTela. Why would someone do that just for select boxes?? Beats me
Thanks everybody for the responses. They taught me something indeed.