2

Writing a jQuery function to test the class of a <td> and act accordingly. The function works by clicking the img inside the <td> and uses the class information of the td to test.

$(document).ready(function () { $('td img').click(function () { if ((this).parent().hasClass('x')) { alert("Seat " + ((this).parent().attr("id")) + " is taken"); } else if ((this).parent().hasClass('selected')){ $(this).attr('src', 'images/a.gif'); $(this).parent().removeClass('selected'); alert($(this).parent().attr("class")); return false; } else { $(this).attr('src', 'images/c.gif'); $(this).parent().addClass('selected'); alert($(this).parent().attr("class")); return false; }; }); }); 

The error says that the method for the image does not exist, despite me specifying the parent.

1 Answer 1

2

There is a $ sign missing.

If($(this).parent()... 

What happens here is that the click function passes the dom/html object instead of the jQuery object and thus you have to build the object with the jQuery function in order to have the correct object to work with!

Sign up to request clarification or add additional context in comments.

1 Comment

Schoolboy error. I never would've spotted it either, thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.