Home » Javascript » javascript jquery find multiple hidden input without specific attribute

javascript jquery find multiple hidden input without specific attribute

Posted by: admin July 12, 2018 Leave a comment

Questions:

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>
Answers: