Click here to Skip to main content
16,016,759 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I have hidden field like

<input id="mainage[1]" type="hidden" name="mainage[1]" value="1"></input>

and want to check from jquery or javascript if it exist

how to do it
Posted

JavaScript has a document.contains function that returns either true or false, try this,

XML
<!DOCTYPE html>
<html>
<body>

<p>A demo page.</p>

<button onclick="myFunction()">Try it</button>

<input id="mainage[1]" type="hidden" name="mainage[1]" value="1"></input>

<script>
function myFunction()
{

var element=document.getElementById("mainage[1]");
document.write(document.contains(element));
}
</script>

</body>
</html>
 
Share this answer
 
v3
Comments
Peter Leow 27-Nov-13 2:43am    
Great that my solution helps, please remember to Accept Answer. Thanks.
SQL
You can use length to see if your selector matched anything.

if ($('#MyId').length > 0) {
    // dostuff
}
 
Share this answer
 
What you can do is you can have a common class name for all those hidden fields

say like this
<input id="mainage[1]" type="hidden" name="mainage[1]" value="1" class="hidden_field"></input>
<input id="mainage[2]" type="hidden" name="mainage[2]" value="2" class="hidden_field"></input>
.
.
.Goes on



so I can write
if($(".hidden_field").length &gt;1){
         // Do what you wanna make
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900