0

i wanna get value from select option on checkbox is checked.

HTML :

<tr>
<td><input type="checkbox" value="1" class="sum">
Data 1 :
<select name="valid">
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
</tr>

<tr>
<td><input type="checkbox" value="2" class="sum">
Data 1 :
<select name="valid">
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
</tr>

JS :

var checked = document.querySelectorAll('input[type=checkbox]:checked.sum');
var values = [];
for(var i = 0; i < checked.length; i++) {
  values.push(checked[i].value);
}
var j = values.join(',');
var jVal = 'im confused here, i need get value from select option on checked checkbox'
window.location=' ?page=token&mode=pass&values=['+join+']&option_val=['+jVal+'] ';

im successful to get value from both (checkbox.checked+select option) but when im not checked the second checkbox, its getting value from all select option. and the question is, how to get value from select option on checked checkbox.

Thanks before,

  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Apr 07 '15 at 11:13

2 Answers2

0
var checkedValue = document.querySelector('.messageCheckbox:checked').value;

jQuery:

var checkedValue = $('.messageCheckbox:checked').val();

Get the value of checked checkbox?

Community
  • 1
  • 1
Grumpy
  • 2,140
  • 1
  • 25
  • 38
0

This is a bit of a crude solution. You have to use data-select to manually specify the id of the <select> element.

HTML

<input type="checkbox" data-select="select1">

<select id="select1">
    <option value="1">1</option>
    <option value="2">2</option> 
</select>

<input type="checkbox" data-select="select2" checked>

<select id="select2">
    <option value="1">1</option>
    <option value="2">2</option> 
</select>

JS

var checked = document.querySelectorAll('input');
var values = [];
for(var i = 0; i < checked.length; i++){
    if(checked[i].checked){
         var select_id = checked[i].getAttribute("data-select");
         values.push(document.getElementById(select_id).value);
    }
}
mkaminsky
  • 353
  • 2
  • 10