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

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
  • Could take a look at: https://stackoverflow.com/questions/5866169/how-to-get-all-selected-values-of-a-multiple-select-box – Martin Aug 27 '18 at 12:59

4 Answers4

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.

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
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>
KarelG
  • 5,176
  • 4
  • 33
  • 49
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 );
}
Jasmin Mistry
  • 1,459
  • 1
  • 18
  • 23
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]
AymDev
  • 6,626
  • 4
  • 29
  • 52