null attribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TJS

    null attribute

    how to check for null attribute in form input field using IE

    this does not work in IE

    var curr_id = tablecells[i].getAttribute(' name');
    if (curr_id != null){
    ....
    }


  • Mick White

    #2
    Re: null attribute

    TJS wrote:[color=blue]
    > how to check for null attribute in form input field using IE
    >
    > this does not work in IE
    >
    > var curr_id = tablecells[i].getAttribute(' name');
    > if (curr_id != null){
    > ...
    > }
    >
    >[/color]

    if(var curr_id = tablecells[i].getAttribute(' name'))

    Mick

    Comment

    • TJS

      #3
      Re: null attribute

      that compares values but doesn't give me the desired result and doesn't
      check to see if it's null


      "Mick White" <mwhite13BOGUS@ rochester.rr.co m> wrote in message
      news:Vytod.1114 2$1u.3531@twist er.nyroc.rr.com ...[color=blue]
      > TJS wrote:[color=green]
      >> how to check for null attribute in form input field using IE
      >>
      >> this does not work in IE
      >>
      >> var curr_id = tablecells[i].getAttribute(' name');
      >> if (curr_id != null){
      >> ...
      >> }[/color]
      >
      > if(var curr_id = tablecells[i].getAttribute(' name'))
      >
      > Mick[/color]


      Comment

      • Rob B

        #4
        Re: null attribute



        Are you simply validating a non-empty field? Text controls have .value
        properties, 'empty' ones contain the null (empty) string, not the null
        value.

        var curr_id = tablecells[i].getAttribute(' value');
        if (/^\s*$/.test(curr_id)) {
        ....
        }

        The regex catches all-space entries as well as empty ones.

        Not clear what that 'tablecells' array contains...


        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Fred Oz

          #5
          Re: null attribute

          TJS wrote:[color=blue]
          > that compares values but doesn't give me the desired result and doesn't
          > check to see if it's null[/color]

          var x = null; // x is set to null, (x == null) is true.
          var x; // x is undefined, (x == null) is true.
          var x; // x is undefined, (x === null) is false.
          var x = ''; // x is empty string, (x == null) is false.
          x; // x is undefined, javascript error.

          Fred.

          Comment

          • Michael Winter

            #6
            Re: null attribute

            On Mon, 22 Nov 2004 17:07:35 -0800, TJS <nospam@here.co m> wrote:

            [Corrected top-post]

            Please don't do that in future.
            [color=blue]
            > "Mick White" <mwhite13BOGUS@ rochester.rr.co m> wrote in message
            > news:Vytod.1114 2$1u.3531@twist er.nyroc.rr.com ...[/color]

            [snip]
            [color=blue][color=green]
            >> if(var curr_id = tablecells[i].getAttribute(' name'))[/color][/color]

            That's a syntax error. The var statement cannot appear in an expression.

            var curr_id = tablecells[i].getAttribute(' name');
            if(curr_id) {
            // curr_id contains the name
            }

            or

            var curr_id
            if((curr_id = tablecells[i].getAttribute(' name')) {
            // curr_id contains the name
            }
            [color=blue]
            > that compares values but doesn't give me the desired result and doesn't
            > check to see if it's null[/color]

            Really, checking for null is incorrect as Element.getAttr ibute cannot
            return null, only a string. However, Microsoft (as usual) have cocked
            things up, allow all manner of types to be returned.

            Mick's suggestion was the right approach, except for the syntax error.
            When you evaluate an expression within an if statement, the result of that
            evaluation is coerced into a boolean. Different types are coerced in
            different ways.

            Boolean -> (no conversion)
            Null -> false
            Number -> false if plus/minus zero (+/-0) or NaN, true otherwise
            Object -> true
            String -> false if empty string (''), true otherwise
            Undefined -> false

            So as you can see, if curr_id was null, it would evaluate to false and the
            block would not be executed.

            There is another reason which I briefly mentioned earlier which makes
            Mick's suggestion the right one: Element.getAttr ibute is not supposed to
            return null, only a string. This would mean that the comparison in
            conforming browsers would be

            if(<string> != null) {

            which would *always* be true, even if the string was empty, which is
            probably not what you want.

            Anyway, the sensible thing to do (assuming you're using HTML, not XML) is
            to avoid Element.getAttr ibute completely. You can achieve the same with

            var curr_id = tablecells[i].name;

            which will always succeed (again, only if you're using HTML).

            By the way:

            1) Table cells cannot have name attributes.
            2) The identifier, curr_id, is a misnomer. Wouldn't curr_name be
            more appropriate, or even better

            var curr_id = tablecells[i].id;

            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail.

            Comment

            • TJS

              #7
              Re: null attribute

              great response, thank you........



              Comment

              Working...