D-W
D-W

Reputation: 5341

jquery Check if hidden fields have values

I have dynamic fields and want to check if the values have been set, cant seem to get it work, no errors, but nothing seems to log. (please note I have other hidden fields on the page, I need to only validate the ones that start with ItemId_)

<div id="List">
<row id="row_bb0acd1a-b1e8-6f8c-88ea-324432339176">
<input type="hidden" id="ItemId_bb0acd1a-b1e8-6f8c-88ea-324432339176">
</row>
<row id="row_aaaaa-b1e8-6f8c-88ea-31223333">
<input type="hidden" id="ItemId_aaaaa-b1e8-6f8c-88ea-31223333">
</row>

</div>
<button type="button" onclick="Validate()")Next</button>

function Validate()
{

 $.each($('#List').find("input[name^='ItemId_']"), function (key, value) {
                console.log(value);
                console.log(key);
                if (value.length == 0) {
                    alert('Please ensure you select a item from the list');
                    return false;
                }
}

Upvotes: 1

Views: 933

Answers (1)

Shiran Dror
Shiran Dror

Reputation: 1480

You are looking for elements with a name attribute starting with ItemId_ while your inputs doesn't have the name attribute. You can change it to id in the jquery script or change the id attribute to name in the elements

function Validate() {
  $.each($('#List').find("input[id^='ItemId_']"), function(key, value) {
    console.log(value);
    console.log(key);
    if (value.length == 0) {
      alert('Please ensure you select a item from the list');
      return false;
    }
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="List">
<row id="row_bb0acd1a-b1e8-6f8c-88ea-324432339176">
<input type="hidden" id="ItemId_bb0acd1a-b1e8-6f8c-88ea-324432339176">
</row>
<row id="row_aaaaa-b1e8-6f8c-88ea-31223333">
<input type="hidden" id="ItemId_aaaaa-b1e8-6f8c-88ea-31223333">
</row>

</div>
<button type="button" onclick="Validate()">Next</button>

Upvotes: 1

Related Questions