SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS does not seem to recognize null hidden value within table

    I have the following code:
    Code:
    <html>
    <head>
    <title>TEST</title>
    <script language="javascript">
    function getFields(index)
    {
    var num=1;
    if (index !=0)
      num=2*index +1;
    return num;
    }
    
    function show(line)
    {
    var  num=getFields(line);
    id=document.forms[0].elements[num-1].value;
    alert("id read with elements[num]="+id);
    if (id == null)
     alert("1. ID NULL");
    
    id=document.forms["0"].elements["serv_"+line].value;
    alert("id read with elements[name]="+id);
    if (id == null)
       alert("2. ID NULL!");
    }
    </script>
    <body>
    <%@page language="java" %>
    <center>
    <form onSubmit="return false" method="post" name="my_form" action="">
    <table border=2>
    <tr>
    <td id='c_0' onclick='show(0)'>
      <input type=hidden value=null name="serv_0">
     <input type=text readonly value="1" size=25>
    </td></tr>
    <tr>
    <td id='c_1' onclick='show(1)'>
      <input type=hidden value="SERV" name="serv_1">
      <input type=text readonly value="2" size=25>
    </td></tr>
    </table>
    </form>
    </body>
    </html>
    Results:
    1. When I click on the 1st cell, the alarms that are being fired are:
    - id read with elements[num] = null
    - id read with elements[name] = null

    AS YOU CAN SEE JS DOES NOT RECOGNIZED NULL!

    2. When I click on the 2nd cell,
    - id read with elements[num] = SERV
    - id read with elements[name] = SERV

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    if (id == "null")

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't want to write such a thing... what if really the string contains "null"!
    Is there another way?

  4. #4
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have you try id.length, since it is treated as string.

    Code:
    if (id.length == 0) {
    
    }

  5. #5
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function isNull(a)
    {
         return typeof a == 'object' && !a;
    }
    
    // snip //
    
    if ( isNull (id) )
    ???
    Ian Anderson
    www.siteguru.co.uk


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •