Javascript-Get selected option in table

how to find the selected value of dropdown in javascript
javascript get selected option text
javascript loop through select options
javascript select option
how to display selected value of dropdownlist in html
get index of selected option javascript
javascript set selected option by value
javascript set selected option by index

I've an HTML table and i want to get a selected value in list in a td.

I proceed like this :

var table = document.getElementById('ZZ');
    for (var r = 0, n = table.rows.length; r < n; r++) {
        for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
            var type =table.rows[r].cells[11].innerHTML;
            alert(type);
        }
    }

And i've a result like this in an alert box

<SELECT id="" onchange=''... <OPTION value=1>A</OPTION><OPTION selected=2>B</OPTION></SELECT>

And i want to get 'B' in a var. How can i complete this?

 html:

    <SELECT id="orange" onchange=''... >
       <OPTION value=1>A</OPTION>
       <OPTION selected=2>B</OPTION>
    </SELECT>

    script:

   var allDropdown =  document.getElementsByTagName("select");
   allDropdown[0].value;
  allDropdown[1].value;

/**********************************************************/

for(var item in allDropdown ){
      alert(allDropdown[item].value)
    } 

Get Value or Selected Option in Select Box Using JavaScript, How to get the value of selected option in select box or reference to it. There are a variety of ways to use JavaScript to obtain the value of the selected option in a select list or <option value="table_class">PHP Table Class</option> <​option  There are a variety of ways to use JavaScript to obtain the value of the selected option in a select list or a reference to the selected option: Use the value property of the select list. Use the selectedIndex property.

Since you say you get the result which you get, you could use Element.querySelector which uses css selectors to target the elements.

var type = table.rows[r].cells[11].querySelector("[selected]").innerHTML;

Note that I'm using innerHTML since there's some variations between textContent and innerText and also due to the fact that <option> cannot contain any html.

JavaScript - Get selected value from dropdown list, A JavaScript example to show you how to get the selected value or text from a dropdown list. A drop box list. <select id="country"> <option  Adds an <option> element into the collection at the specified index. If no index is specified, it inserts the option at the end of the collection. Returns the <option> element from the collection with the specified index (starts at 0). Returns the <option> element from the collection with the specified id.

**

If your select option exist in the inner child of the td of the table and you wanna get the value, this may help you out. It worked for me

**

**HTML**

<table class="table table-borderless" id="all_table_hour">
  <tbody class="table-hours-record">
  <tr data-mytr_id=1 class="table-rowclass1">
    <td>Time</td>
    <td data-mytd1_id=1 class="table-rowtd1class1">
      <div class="time-slot">
        <span >
          <select class="hour_selected_from_all" id="hour_selected_from_all1">    
            <option selected="selected" value="00:00">0 hour</option>
            <option value="01:00">1 hour</option> 
          </select>
        </span>
      </div>
    </td>
    <td >
      <div class="time-slot">
        <input placeholder="$10.00">
      </div>
    </td>
  </tr>

  </tbody>
</table>

**

CODE JS

**

var testTable = document.getElementById('all_table_hour');

for (i = 1; i < testTable.rows.length; i++) {

    var recordcells = testTable.rows.item(i).cells;
    for (var j = 0; j < recordcells.length; j++) {
        record = recordcells.item(j).getElementsByTagName("select")[0].value
        //Now you can add this record as per your required format (list or in json format)
    }
}

HTML DOM Select value Property, value, Specifies the value of an <option> element in a drop-down list that should get selected. If the value does not exist, the drop-down list will display an empty  Questions: How do I get the selected value from a dropdown list using JavaScript? I tried the methods below but they all return the selected index instead of the value: var as = document.form1.ddlViewBy.value; var e = document.getElementById("ddlViewBy"); var strUser = e.options[e.selectedIndex].value; //This is one of the simplest form by (Narendra Kottamidde) : var value

As in your code you are able to get object of TD then you can use "Find" function to find select and take selected option out from the select.
.find("select") will give you select jquery object and then through sudo code used :selected" you can find selected option from list.It will  return you "B".
var table = document.getElementById('ZZ');
for (var r = 0, n = table.rows.length; r < n; r++) {
	for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
		var type =table.rows[r].cells[11].find("select")**.find(":selected").text()**;
		alert(type);// B
		
	}
}

Hope this will help

HTML DOM Option value Property, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  The selectedIndex property sets or returns the index of the selected option in a drop-down list. The index starts at 0. Note: If the drop-down list allows multiple selections it will only return the index of the first option selected. Note: The value "-1" will deselect all options (if any).

HTML DOM Select selectedIndex Property, Note: If the drop-down list allows multiple selections it will only return the index of the first option selected. Note: The value "-1" will deselect all options (if any). Note​  This index starts from 0 and returns -1 if no option is selected. The options property returns the collection of all the option elements in the <select> dropdown list. The elements are sorted according to the source code of the page. The index found before it can be used with this property to get the selected element. This option’s value can

HTMLSelectElement.selectedIndex, The HTMLSelectElement.selectedIndex is a long that reflects the index of the first or last selected option element, depending on the value of  The JavaScript function above takes in two parameters. The unique ID of our select element and the value that we want to select. Inside our function, we loop through each option in the select element. If the value of the option matches the value that we want to select, we change the selectedIndex of the element and break out of the for loop.

HTMLSelectElement.selectedOptions, The list of selected options is an HTMLCollection object with one entry The JavaScript code that establishes the event handler for the button,  JavaScript. var selectElem = document.getElementById('select') var pElem = document.getElementById('p') // When a new <option> is selected selectElem.addEventListener('change', function() { var index = selectElem.selectedIndex; // Add that data to the <p> pElem.innerHTML = 'selectedIndex: ' + index; }) HTMLSelectElement.selectedIndex - Example - code sample.

Comments
  • If i do that i only get one element and i need to do it for all my table.
  • var allSelect = document.getElementsByClassName("selectClassName");
  • Is telling me "null object or undefined".
  • Or Is telling me "The object does not support the property or method "querySelector"".
  • Tried and "The object does not support property or "find" method".