0

Can javascript or jquery create an array of values from multiple hidden inputs with randomly created ids (in other words, no specific attribute to search for)? The code below only results in the alert of the first hidden input, 'abc'...
Thanks

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="some_random_id" value="abc" />
<input type="hidden" id="some_random_id" value="def" />
<input type="hidden" id="some_random_id" value="ghi" />

<script>
//create hidden fields array
var hiddenFields = [];

//for each table row
$('html').each(function()
{
  //get hidden field 
  if (hiddenField != $(this).find("input[type='hidden']").val()){
  var hiddenField = $(this).find("input[type='hidden']").val();
  }
  //if not empty push to array
  if(hiddenField!='undefined'&& hiddenField !=null )
    hiddenFields.push(hiddenField);
});
alert(hiddenFields);
</script>

2
  • Why are you doing each on the html element? Commented May 23, 2018 at 0:07
  • You can't have multiple elements with the same ID or you'll only return the first one. And where is your body? Commented May 23, 2018 at 0:08

4 Answers 4

2

Maybe try this:

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="some_random_id" value="abc" />
<input type="hidden" id="some_random_id" value="def" />
<input type="hidden" id="some_random_id" value="ghi" />

JS

var tags = document.getElementsByTagName("input");

for(var i = 0; i < tags.length; i++){
  if(tags[i].getAttribute("hidden") == null){
    console.log(tags[i].value);
  }
}

Codepen - https://codepen.io/anon/pen/jxRVMb?editors=1010

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

Comments

1

You're only calling .val once after you .find, so it only returns the value of the first element in the jQuery collection. ($('html').each will only iterate once, because there's only one html tag in the document)

You can try something like this instead, no jQuery needed:

const hiddenFields = [...document.querySelectorAll('input[type="hidden"]')]
  .map(input => input.value);
  
console.log(hiddenFields);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="some_random_id" value="abc" />
<input type="hidden" id="some_random_id" value="def" />
<input type="hidden" id="some_random_id" value="ghi" />

You should also try to fix the HTML so that there are no duplicated IDs; that's invalid.

If you wanted to use jQuery iteration:

const hiddenFields = $.map($('input[type="hidden"]'), input => $(input).val());
console.log(hiddenFields);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="some_random_id" value="abc" />
<input type="hidden" id="some_random_id" value="def" />
<input type="hidden" id="some_random_id" value="ghi" />

1 Comment

Thanks 4 quick response and using both! Yea, was just trying to simplify the question visually..the id obviously shouldn't be the same.
0

Can use a combination of filter() and map()

var results = $("input[type='hidden']").filter(function() {
  return this.value // only return elements that have value
}).map(function() {
  return this.value // pass the value to array
}).get()

console.log(results)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="some_random_id" value="abc" />
<input type="hidden" id="some_random_id" value="def" />
<input type="hidden" id="some_random_id" value="ghi" />

1 Comment

This worked too, thanks! I up-voted as well, but since I'm new user, will show up later when i have some activity.
0

Grab all hidden inputs and then you can fetch value by iterating on it using forEach loop

const hiddenInputs = document.querySelectorAll('input[type="hidden"]');

const hiddenInputValues = [];

hiddenInputs.forEach((ele) => {
    hiddenInputValues.push(ele.value);
});

console.log(hiddenInputValues);
<input type="hidden" id="some_random_id" value="abc" />
<input type="hidden" id="some_random_id" value="def" />
<input type="hidden" id="some_random_id" value="ghi" />

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.