[ACCEPTED]-use JQuery to get siblings-jquery

Accepted answer
Score: 17

Your issue stems from your approach. Calling 19 a function directly won't do what you're 18 expecting, which is to have a jquery object 17 representing the <a> tag. Rather than 16 call a function directly you can register 15 a function to respond to a click event on the 14 <a>.

First, give your link an id:

<a href="#" id="appendSelect">append</a>

Then 13 use replace your appendField() function with a jQuery 12 select that responds to click

$(document).ready(function() {
  $("#appendSelect").click(function() {
    var $thatInput = $(this).siblings("input");
    var $selectValue = $(this).siblings("select").val();

With this approach 11 $(this) will represent your <a>.

You can see 10 what I'm talking about to a greater depth 9 by using a javascript debugging console 8 (chrome has one by default, and you can 7 use Firebug on Firefox). Try your original 6 function based approach again and add a 5 console.log($(this)); statement. You should see it printing out 4 a DOMWindow object. Now put the same log 3 statement in the click function and you'll see 2 that jQuery has given you a $(this) representing 1 what you expected it to be.

Score: 4




Score: 1

The appendfield method does not access to 1 $(this).

Here is the working code: http://jsfiddle.net/NBf4d/2/

Score: 0

use .children() or .find()

.children only 2 looks for direct children of the parent, find 1 looks at all levels below the parent.


Score: 0

This will only work for these IDs, but you 1 can easily change them:

Score: 0

try this...



More Related questions