John Smith
John Smith

Reputation: 6259

Add text to input using jquery

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!

Upvotes: 0

Views: 97

Answers (2)

mishik
mishik

Reputation: 10003

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.

Upvotes: 2

Praveen
Praveen

Reputation: 56509

Try this

var yourVal = "someText";

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

Upvotes: 0

Related Questions