0
<form onsubmit = "return checkForm(this)" method="POST" action = "">
<select name="user[11]">
     <option value='1'>one</option>  
     <option value='2'>two</option> 
</select>

<select name="user[12]">
     <option value='1'>one</option>  
     <option value='2'>two</option> 
</select>

</form>

I need to get select box values with keys (which i will use for update user)

im using below function

function checkForm(frm){
  var inps = document.getElementsByName('user[]');
  console.log(inps );// but its displaying blank nodelist
}

Please give your valueable suggestions

1

4 Answers 4

2

You can use document.querySelectorAll() and a wildcard for the name attribute:

[name^="user["]

This means, find all elements that have a name property starting with user[.

If you want to get the keys from the name property, map it like so:

userArray.map(element => Number(element.name.match(/\d+/)[0]));

let userArray = Array.from(document.querySelectorAll('[name^="user["]'));

let userArrayKeys = userArray.map(element => Number(element.name.match(/\d+/)[0]));

console.log("Matched elements",userArray);

console.log("Their Keys", userArrayKeys);
<form onsubmit="return checkForm(this)" method="POST" action="">
  <select name="user[11]"> <!-- I will get selected -->
    <option value='1'>one</option>
    <option value='2'>two</option>
  </select>

  <select name="user[12]"> <!-- I will get selected -->
    <option value='1'>one</option>
    <option value='2'>two</option>
  </select>
  
  <select name="update[12]"> <!-- I will not get selected -->
    <option value='1'>one</option>
    <option value='2'>two</option>
  </select>
</form>

You should give your input fields names that better reflect what data they hold, though.

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

3 Comments

Thanks Luca by this i am able to check values can you suggest a way to get keys also.
@Luca Why we not use getElementsByName()
@BharatDangar Because getElementsByName, to my knowledge, neither supports substring matching, nor a native method for getting elements that have an array-like name construct
1

That is a weird tag naming (I do not recommend the use of [ and ] in attribute values.) You can solve it by using document.querySelectorAll which allows you to select elements with CSS selectors.

I have used

document.querySelectorAll('select[name^="user["]')

The ^= is a part of the 6.2. Substring matching attribute selectors. [name^="user["] means "select where the name attribute begins with "user[".". This will select elements with user[1] but also user[2] or user[2][1] or users[hello] or users[hello or users[hello...

console.log(document.querySelectorAll('select[name^="user["]'));
<form onsubmit = "return checkForm(this)" method="POST" action = "">
<select name="user[11]">
     <option value='1'>one</option>  
     <option value='2'>two</option> 
</select>

<select name="user[12]">
     <option value='1'>one</option>  
     <option value='2'>two</option> 
</select>

</form>

Comments

0

YOu have to select by below

function checkForm(frm){
  var inps1 = document.getElementsByName('user[11]');
  console.log(inps1 );
  var inps2 = document.getElementsByName('user[12]');
  console.log(inps2 );
}

Comments

0

Except if the user indexes are needed you can skip them like:

<select name="user[]">...</select>
<select name="user[]">...</select>

<!-- $_POST values:
    [user] => Array
    (
        [0] => 1
        [1] => 1
    )
-->

And then get them as you tried:

console.log(document.getElementsByName('user[]'));
// NodeList(2) [select, select]

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.