0

I would like that jquery adds the value of a class="num" to the form with the id="form2" when the class="text" is clicked. Its important that only the value of the next "num"-class after the "text"-class is added to the form!

<p class="text" >
<a class="num"><%= g.nummer %></a><a class="bez"><%= g.bezeichnung %></a>
</p>

<input id="form2" name="suche" type="text">

My actual code:

$(document).on("click", ".text", function() {
   $("#Content").html($(this).next(".text1").html());
   $("#form2").html($(this).next(".num").val());
   $('.text').css('color','');
   $(this).css( "color", "red" );
  });
 });

But somehow this line wont work:

$("#form2").html($(this).next(".num").val());

Thanks!

1
  • 2
    change .html() to .val() Commented Aug 19, 2013 at 13:34

2 Answers 2

2

Your .num is a child of .text, rather than next element.

Change:

$("#form2").html($(this).next(".num").val());

To:

$("#form2").val($(this).children(".num").html());

and as correctly said in comments: use val() to get/set value of input element, and .html() to get/set contents of the element.

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

Comments

0

Try this

var yourVal = "someText";

$("#form2").children(".num").val(yourVal);

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.