Question

Anyone know why the following JQuery expression works in Firefox but not in IE or Chrome?

$('form :hidden:last').attr('name')

An alert statement reveals that in IE the expression is undefined.

UPDATE: Here is some HTML that fails.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
   <head>
      <script type="text/javascript" src="/js/jquery-1.3.2.min.js">
      </script>
      <script type="text/javascript">
         function addItem() {
           var v = $('form :hidden:last').attr('name');
           var n = /(.*)input/.exec(v);
           var newPrefix;
           if ( n[1].length == 0 ) {
             newPrefix = '1';
           } else {
             newPrefix = parseInt(n[1])+1;
           }
           var lastExercise = $('form tr:last select:first').attr('value');
           var oldElem = $('form tr:last');
           var newElem = oldElem.clone(true);
           var lastHidden = $('form :hidden:last');
           lastHidden.val(newPrefix);
           var pat = '=\"'+n[1]+'input';
           newElem.html(newElem.html().replace(new RegExp(pat, 'g'), '=\"'+newPrefix+'input'));
           newElem.appendTo('table');
           $('form tr:last select:first').attr('value', lastExercise);
           $('form :hidden:last').val('');
         }
         function removeItem() {
           var rows = $('form tr');
           if ( rows.length > 2 ) {
             rows.filter(":last").remove();
             $('form :hidden:last').val('');
           } else {
             alert('Cannot remove any more rows');
           }
         }
      </script>
   </head>
   <body>
         <form autocomplete="off" method="post" action="">
            <div>
               <input type="button" onclick="addItem(); return false;" value="Add Item" />
               <input type="button" onclick="removeItem(); return false;" value="Remove Last Item" />
            </div>
            <table>
               <tr>
                  <th>Name</th>
                  <th>Weight</th>
               </tr>
               <tr>
                  <td>
                     <input type="text" name="input1" id="input1" value="" />
                  </td>
                  <td>
                     <input type="text" name="input2" id="input2" value="" />
                     <select name="input3" class="unitsInput">
                        <option value="0">
                           Pounds
                        </option>
                        <option value="1">
                           Kilograms
                        </option>
                     </select>
                  </td>
                  <td>
                     <input type="hidden" name="input4" id="input4" value="" />
                  </td>
               </tr>
            </table>
            <div>
               <select name="input8">
                  <option value="0">0</option>
                  <option value="1">1</option>
                  <option value="2">2</option>
               </select>
            </div>
            <div>
               <input type="submit" name="submit" id="submit" value="Submit" />
            </div>
         </form>
   </body>
</html>

No correct solution

OTHER TIPS

Is your JQuery wrapped in the a $(document).ready( ... ) ?

For example:

$(document).ready(function() {
    $('form :hidden:last').attr('name')
});

It is essential to do this to ensure that the DOM has fully loaded before your JQuery code starts executing. Otherwise, there is no DOM (document) for it to query.

Do you run this query after DOM is fully loaded and using the latest version of jQuery? The above example from Dan works fine even in IE6 for me.

I was able to get it to run fine in IE7.
What does your HTML look like?

Here is what mine looks like:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
</head>

<body>
<form action="" method="get" name="testform">
<input name="bob" type="hidden" value="" />
<input name="steve" type="hidden" value="" />
<input name="william" type="hidden" value="" /></form>
<script type="text/javascript">
    $(document).ready(function(){
        alert($('form :hidden:last').attr('name'));
    });
</script>
</body>
</html>

It appears that IE treats the option elements at the end as hidden. Changing all the "...:hidden..." selectors in the javascript to "...input:hidden..." seems to solve the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top