Reputation: 9338
I want to apply value from input to select when document is ready. Everything works fine.
Do you recommend to check if both select#city
and input#city_hidden
exist to make this code better?
$(document).ready(function ()
{
$("select#city").val($("input#city_hidden").val());
});
Upvotes: 0
Views: 86
Reputation: 841
If you really want to check if items exist in a page you can use .length in an if statement.
if($('#myDiv').length){
//#myDiv exists because the length is not 0
}
here's a jsFiddle: http://jsfiddle.net/Ajvp7/
Upvotes: 0
Reputation: 16942
If you are 100% sure that $("input#city_hidden") and $("select#city") are always there - there is no reason to check. If they are a part of your html I would say a check is not necessary.
Upvotes: 0
Reputation: 28154
The decision depends on numerous factors, not just this line of code.
Technically, you don't need to check if select#city exists. If it doesn't, jQuery will just do nothing as the $("select#city") selector will be empty.
Upvotes: 1