Skip to content Skip to sidebar Skip to footer

Jquery Addoption And Selectoptions

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 n

Solution 1:

use Jquery Append to add options like this

$("yourid/class here").append($("<option></option>").attr("value", youroption-value).text(youroption-text));

Solution 2:

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.net/maverickosama92/rGzPS/1/

Solution 3:

Finally found whats wrong with it. These methods come 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.

Post a Comment for "Jquery Addoption And Selectoptions"