Javascript - check object defined

Collapse
X
 
  • Time
  • Show
Clear All
new posts

  • Dormilich
    replied
    yes ... i think this is correct ... document.getEle mentById('nodeI d') returns null when the element is not found
    just went to look it up (DOM level 2 Core) … it indeed returns null if the element doesn’t exist.

    so you could also test (unless the DOM implementation is faulty)
    Code:
    var a = document.getElementById('VE10110INTEFT');
    if (a === null) { /* … */ }

    Leave a comment:


  • gits
    replied
    yes ... i think this is correct ... document.getEle mentById('nodeI d') returns null when the element is not found ... so the check for null is sufficient.

    kind regards

    Leave a comment:


  • ismailc
    replied
    Hi,

    I got it going using "!= null"

    I did try the below, but i would get the error on the first line when defining the var.

    var a = document.getEle mentById('VE101 10INTEFT');
    if (a === undefined)

    Would also not work.
    if (document.getEl ementById('VE10 110INTEFT') === undefined)

    Thanks I'm okay on this topic :)
    I appreciate your help & time

    Leave a comment:


  • Dormilich
    replied
    then do
    Code:
    var a = document.getElementById('VE10110INTEFT');
    if (a === undefined) { /* … */ }

    Leave a comment:


  • ismailc
    replied
    Hi,
    Thanks my problem was that the object was not created or loaded due to some business rules on the page & therefore i would get error on if condition to check value

    Regards

    Leave a comment:


  • Dormilich
    replied
    if an input is not filled, it returns "" (empty string).

    Leave a comment:


  • ismailc
    replied
    I got it going :)

    Code:
          var a = document.getElementById('VE10110INTEFT');
    
          if (a != null)
          { 
            alert("yes");
          }
          else
          {
            alert("no");
          }

    Leave a comment:


  • ismailc
    started a topic Javascript - check object defined

    Javascript - check object defined

    Good day,

    I thought i had this sorted but it does not seem to check if the object is undefined / not created.

    I'm trying to check if object is undefined & i tried a few methos but no luck
    with the "value;" it returns the alert no to say that it is define where it is not

    Please help

    Code:
          var a = document.getElementById('VE10110INTEFT').value;
          if (a === undefined)
          { alert("yes");} else { alert("no");}
    
    var a = document.getElementById('VE10110INTEFT').value;
    if (typeof(a) === undefined)
    
    var a = document.getElementById('VE10110INTEFT');
    if (typeof(a) == "undefined")
    
    var a = document.getElementById('VE10110INTEFT');
    if (a == "undefined")
Working...