views:

333

answers:

2

Hello I'm trying something .. I was reading Jquery traversing documentations some forums, this websites previously asked questions relating this topic .. none actually answers my question so I'll ask instead .. here is the deal

  <div class="entry">
week
<span>some text</span>
<p>DKK</p>
<input type="radio" name="red<% Response.Write(counter); %>" id="radio2" value="75" />

</div>

So I've used a while loop to create bunch of these so I don't have to manually copy paste them.. asp generates them .. now you see the p section it says DKK .. now in order for me not to write everything manually I'd like every divs , p to append with the value of input inside that div , now here is what I did with Jquery

$(document).ready(function() {
     $("input").each(function () {
                 var element_value = $(this).val();
                 $(this).closest("p").append(element_value + ",-");
                  }); 
 });

So what I tried to do is to go trough each input and collect their value, then find closest p tag and append it with its value, now the correct result for this example above would be

<p>DKK 75,-<p>

because input value is 75 and it has included in append function ,- add that as well, this code is not working, however if I put

$(this).parents("div.entry").append(element_value + ",-");

instead of

$(this).closest("p").append(element_value + ",-");

The correct results appear but not exactly where I want them .. hopefully someone can help me, thank you

+5  A: 

If you read the documentation for closest() you will see it only looks upwards, not sideways:

Get a set of elements containing the closest parent element that matches the specified selector, the starting element included.

You could do this to find the previous <p> from the input:

$(this).prev("p").append(element_value + ",-");

Or a more explicit:

$(this).closest("div.entry").find("p").append(element_value + ",-");

Which would then not depend on which side of the <p> the input happens to be on.

Paolo Bergantino
Excellent lad, thank you .. I did read the documentation but not the right links I guess thank you for your swift answer :D
c0mrade
A: 

try this:

$(this).parents("div.entry").find('p:first').append(element_value + ",-");
pixeline