1

I have the following HTML in a page:

<a href="javascript:void(0)" class="move-next"><img src="/Content/images/small-next-arrow.png" height="16" width="16" alt="Next status arrow" title="Progress to next status"/></a></div>

and I have the following javascript that attempts to handle the click event:

function InitProgressionSelectors() {
$(".move-next").click(function() {
    moveNext(this);
});

$(".move-previous").click(function() {
    movePrevious(this);
});
}

function moveNext(target) {
    var sourceContainer = target.parent("td");
    var targetContainer = sourceContainer.next("td");
}

I'm obviously missing something, because "target" in the moveNext function is returning an HTMLAnchorElement, but when I try to wrap that, or access it somehow as a jQuery object, so I can try to get a handle to it's parent container, I get errors.

A reference to $(target) returns null. How do I get a reference to target as a jQuery object so I can work with it in that context? What am I missing?

Thanks.

4
  • Could you perhaps post a demo that reproduces your problem at JS Fiddle or JS Bin? Commented Dec 5, 2010 at 19:39
  • 1
    idon't remeber whether parent and next belongs to jquery api or regular javascript , right now you are just passing this and using this.parent , if you want to use jquery , you need to use $(this).parent /// Commented Dec 5, 2010 at 19:43
  • Wow...I'm an idiot. @gov - You were right, I needed to be passing $(this), not just "this". That got me where I needed to be. Thanks. Commented Dec 5, 2010 at 19:48
  • you can give a vote , if you like my answer... Commented Dec 5, 2010 at 19:50

3 Answers 3

3

You need pass the jquery object

$(".move-previous").click(function() {
    movePrevious($(this));
});
Sign up to request clarification or add additional context in comments.

Comments

1

As I mentioned in the comments below, using $(target) didn't work. However, as can be seen in the comments on the original question above, the solution was to actually pass $(this) as the argument. Once I did that, target.parent resolved to the expected node, and worked.


bob can you try using dollor

$(target)

var sourceContainer = $(target).parent("td");

2 Comments

"A reference to $(target) returns null" ...so, $(target).parent("td") returns an error.
@bob can you do a alert before calling the function do a alert(this) , it should print object object///otherwise you have to post the html in jsfiddle
0

Another alternative is to pass the function in by reference:

$(".move-next").click(moveNext);

You can then use the selector as expected:

function moveNext() {
    var sourceContainer = $(this).parent("td");
    var targetContainer = sourceContainer.next("td");
}

Please note the lack of parentheses when passing the function to the click handler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.