views:

217

answers:

4

I have a table layed out like this:

        <td>
                somename
        </td>
        <td class="hoverable value" >
                somevalue
        </td>
        <td class="changed">

        </td>
        <td class="original value">
            <input type="hidden" value="somevalue" />
        </td>

And what I'm trying to do is, I hover over the hoverable td which turns it into a textbox. Once I hover out I want to check the hidden field for it's original value and put an image in changed if the 2 are different from each other. I already have this:

$(document).ready( function() {
    var newHTML = '';

    $('table td.hoverable').hover(
    function () {
        var oldHTML = $(this).html().trim();
        $(this).html('<input type=\'text\' value=\'' + oldHTML + '\' size=\'' + ((oldHTML).length + 2) +'\' />');
    },
    function() {
        newHTML = $('input', this).val();
        var oldHTML = $(this).next('td.original').children('hidden').val();
       if(newHTML != oldHTML) {
            $(this).next('td.changed').html('Changed');
        }
        $(this).html(newHTML);
    })
});

but it doesn't work. What fails apparently is grabbing the value of the hidden field, and I've tried selecting it in several different ways but just can't get to it. Any ideas or tips are gratefully appreciated ;)

+1  A: 

Try

$(this).next('td.original').find('input:hidden').val();
Ken Redler
Nope, it keeps returning 'undefined'
Fverswijver
A: 
var oldHTML = $(this).next('td.original').children(':hidden').val();
Anatoly G
also returns 'undefined'
Fverswijver
A: 

Replace

var oldHTML = $(this).next('td.original').children('hidden').val();

With

var oldHTML = $(this).next('td.original').find('input:hidden').val();
Kerry
+1  A: 

You need to use .nextAll()

var oldHTML = $(this).nextAll('td.original').find('input:hidden').val();

Why? Because .next() is taking the next element and seeing if it matches the selector. If you use .nextAll() you get the next element that matches the selector.

ICodeForCoffee
works like a charm! Thanks a lot! Good to know the difference.
Fverswijver
The jQuery documentation says .next() works exactly this way, but not in a way that it's intuitively understood. This behavior threw me when I tried to do the exact same thing recently.
ICodeForCoffee