Javascript - check object defined

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ismailc
    New Member
    • Sep 2006
    • 200

    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")
  • ismailc
    New Member
    • Sep 2006
    • 200

    #2
    I got it going :)

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

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      if an input is not filled, it returns "" (empty string).

      Comment

      • ismailc
        New Member
        • Sep 2006
        • 200

        #4
        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

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

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

          Comment

          • ismailc
            New Member
            • Sep 2006
            • 200

            #6
            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

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              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

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                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) { /* … */ }

                Comment

                Working...