-4
I lost a little time with a validation in javascript and I got the doubt in my head: I need to know if an element exists through id.
I tried a few ways and I was unsuccessful:
//tentativa 1
$('#idDiv').val() != undefined
//tentativa 2
typeof($('#idDiv').val()) != 'undefined'
//tentativa 3
var varIdDiv = $('#idDiv').attr("value");
if (varIdDiv) { /*código maroto*/ }
Finally I found a way to validate that worked:
if ($('#idDiv').length > 0) { /*código maroto*/ }
I understand that in languages such as C#, the validation looks more like the forms that did not work (checking if the object exists/not null).
My question is what would be the most effective way to validate whether an object exists via javascript/jquery?