JS does not seem to recognize null hidden value within table

Discussion in 'JavaScript' started by celia05es, Apr 18, 2007.

  1. #1
    I have the following 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>
    
    Code (markup):
    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
     
    celia05es, Apr 18, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    You should enclose your attribute values using double quotes, and instead of using null you should use an empty string: ""
    So, you should check against that empty string "", not null.

    replace this:
    
    <input type=hidden value=null name="serv_0">
    <input type=text readonly value="1" size=25>
    
    Code (markup):
    with this:
    
    <input type="hidden" value="" name="serv_0">
    <input type="text" readonly="true" value="1" size="25">
    
    Code (markup):
     
    ajsa52, Apr 18, 2007 IP
  3. celia05es

    celia05es Guest

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you! I should have thought about it!!!!
     
    celia05es, Apr 19, 2007 IP