Ztyk Ztyk - 1 month ago 6
jQuery Question

JQuery get the option of a checkbox checked

I'm doing some code where i need to get the option(value) from an checkbox checked

Here is how i create the checkbox

function createCheckbox(txt) {
var comb = document.createElement("Select");
comb.className = "something";
for (var i = 0; i < txt.length; i++) {
var option = document.createElement("OPTION");
var optionText = document.createTextNode(txt[i]); //some options
option.appendChild(optionText);
comb.appendChild(option);
}
return comb;
}


Function where i need to show the option that is selected

function foo{
var inputTextValue = document.getElementsByClassName("something");
var checkedValue = $(inputTextValue).is(':checked');
alert(checkedValue); //true/false
}


the alert only show true /false depeding if the checkbox is checked or not.But what i need its the option checked. I already tried $(inputTextValue).is(':checked').val().

Answer

Checkbox and Select are two different HTML Elements

For Getting the value of Selected option use

$(".something option:selected").val();