javascript - Find parent() of control, then find("a") JQuery - Stack Overflow

admin2025-04-19  0

I have a <td> that looks something like this:

<td class="myTDClass">
    <span class="spanClass">Header</span>
    <br />
    <img class='myImages' style='cursor: hand;' onclick='doSomething(control)' alt='this is my alt Text'></img>
    <br />
    <span style="color: #123456;">More Text</span>
    <br />
    <a class='findThisClass'>Insert Alt Text Here</a>
</td>

This calls some jQuery code that sets the alt tag to some new text. Then, i want it to set the <a> element to some new text. Here is the jQuery code:

doSomething = function(control)
  {
    var $myControl = $(control);
    $myControl.attr("alt", "This is the new Alt Text!");

    var $newControl = $(control).parent().siblings().find(".findThisClass").first();
    alert($newControl.find("a").text());
  });

The jQuery code sets the alt tag great, but doesn't find the <a class=findThisClass />.

I think I'm using the .parent() and .siblings() wrong. Can anyone find my error? The idea here is that I can search just inside the <td> for the <a> element, and not touch the other elements in other <td>s

I have a <td> that looks something like this:

<td class="myTDClass">
    <span class="spanClass">Header</span>
    <br />
    <img class='myImages' style='cursor: hand;' onclick='doSomething(control)' alt='this is my alt Text'></img>
    <br />
    <span style="color: #123456;">More Text</span>
    <br />
    <a class='findThisClass'>Insert Alt Text Here</a>
</td>

This calls some jQuery code that sets the alt tag to some new text. Then, i want it to set the <a> element to some new text. Here is the jQuery code:

doSomething = function(control)
  {
    var $myControl = $(control);
    $myControl.attr("alt", "This is the new Alt Text!");

    var $newControl = $(control).parent().siblings().find(".findThisClass").first();
    alert($newControl.find("a").text());
  });

The jQuery code sets the alt tag great, but doesn't find the <a class=findThisClass />.

I think I'm using the .parent() and .siblings() wrong. Can anyone find my error? The idea here is that I can search just inside the <td> for the <a> element, and not touch the other elements in other <td>s

Share Improve this question edited Jul 6, 2012 at 19:22 ayyp 6,6184 gold badges34 silver badges47 bronze badges asked Jul 6, 2012 at 19:21 JeffJeff 1,8008 gold badges31 silver badges54 bronze badges 4
  • Why do you need the .parent()? The <a> is a sibling of the <img>. Try taking parent() out of $newControl and see what happens. – ayyp Commented Jul 6, 2012 at 19:24
  • cursor: hand;? Do you mean cursor: pointer;? – gen_Eric Commented Jul 6, 2012 at 19:24
  • 2 @Rocket cursor: hand was a special cursor style for MSIE somewhere =< 6. – VisioN Commented Jul 6, 2012 at 19:26
  • 1 BTW, what is control as an argument for doSomething in onclick? – VisioN Commented Jul 6, 2012 at 19:27
Add a ment  | 

4 Answers 4

Reset to default 2

Assuming you want to find the <a> that's in the same table cell as your <img>, you don't need to use parent(). All you need to do is call .siblings Try:

var $newControl = $(control).siblings('a.findThisClass').first();

This assumes control points to the image within the table cell.

$("a.findThisClass", $myControl).first()

a is a sibling of the image, so, simply:

$(control).siblings(".findThisClass")

Try this instead:

var $newControl = $(control).closest('.myTDClass').find(".findThisClass");
alert($newControl.text());

Also img is self closing tag, instead of:

<img></img>

Just use:

<img... />

And instead of:

onclick='doSomething(control)'

Use:

onclick='doSomething(this)'

so that jQuery has really a control to work with :)

Working Example

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745075446a283541.html

最新回复(0)