1

I want to get Select Element Array value using javascript but i cant get i get all other element value like text etc but cant get select array value.

Select element in My Html code

<select name="abc[]"></select>
<select name="abc[]"></select>

and Javascript Code for get Select Value

document.querySelectorAll("input[name='abc[]']")

but cant get its value, Help me out of this

2
  • Try document.querySelectorAll("select[name='abc[]']"). Commented Apr 14, 2020 at 7:21
  • and how to get text of that selected option? Commented Apr 14, 2020 at 7:28

2 Answers 2

1

First: use select, not input in your css selector. Here's a snippet to find the value and text of the selected option:

const selectedOption = document.querySelector(`select[name='abc[]'] > option:checked`);
console.log(`Value: ${selectedOption.value}, Text: ${selectedOption.textContent}`);
<select name="abc[]">
  <option value="1">text 1</option>
  <option value="2">text 2</option>
  <option value="3" selected>text 3</option>
  <option value="4">text 4</option>
</select>

Sign up to request clarification or add additional context in comments.

Comments

0

Try this : [1]: https://jsfiddle.net/phiphophe/c7a3gdtn/1/

var matches = document.querySelectorAll("select[name='abc[]']");

for(var select of matches){
console.log(select.value);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.