views:

147

answers:

5

The following code snippet works in FF but not on IE8. Prev is returning correct element but not as an object that supports val(). Hence, I can not run .val() on it.

  <input type="hidden" name="data[De][1]" value="1" id="De1" />
   <td class="dr_name">document1</td>
   <td class="remove_dr" colspan="2" align="left">
  <a href="javascript:void(0)">Remove</a></td>

<script>
$(".remove_dr").live('click', function(e) {
  e.preventDefault();
  var value = $(this).prev().prev().val();
  alert(value);
}
</script>

Any help or a better piece of code is appreciated.

A: 

You're missing the closing paren on your call to live():

$(".remove_dr").live('click', function(e) {
  e.preventDefault();
  var value = $(this).prev().prev().val();
  alert(value);
});
wsanville
+2  A: 

2 Things:

  1. You can't have an <input> beside a <td>
  2. If you could (and you can't!), use siblings:

    var value = $(this).parent().siblings("input:hidden").val();

You need to relocate that hidden input to be at a valid location.

Nick Craver
Firefox does tend to be more tolerant of invalid markup... which can be both good and bad.
Matt
Thanks. The hidden field had to be moved.
AA
A: 

You need one more prev call assuming they are all at the same level... but they arent even siblings. the input is not inside the same td as the a so its not going to work. furthermore that input needs to be within a td yu cant jsut have it randaomly in a table structure.

prodigitalson
A: 

While using live events I've observed that there is a difference for Firefox and IE. That is why I use this function to get the same event target throughout all browsers:

getEventTarget = function(e){
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) targ = targ.parentNode; 
    return targ;
}

so inside

$(".remove_dr").live('click', function(e) {
  eventTarget = getEventTarget(e);
  // e.preventDefault(); you can return false instead
  var value = $(eventTarget).prev().prev().val();
  alert(value);
  return false;
});

hope that this is what your looking for

Mike P.
A: 

I found a case where prev() wasn't working as expected on IE8. I find that using prevAll("selector:eq(0)") did work as a substitute for prev("selector").

Frank Schwieterman