I lost a little time with a javascript validation and I had the doubt in my head: I need to know if an element exists through id .
I tried some ways and I did not succeed:
//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 what worked:
if ($('#idDiv').length > 0) { /*código maroto*/ }
I understand that in languages like C # , the validation looks more like the shapes that did not work (checking if the object exists / not null).
My question is what would be the most effective way to validate if an object exists via javascript / jquery ?