Samantha J T Star
Samantha J T Star

Reputation: 32798

How can I set the value of an input within a DIV class?

I have the following:

<td class="ui-widget-content" style="width: 244px;">
   <span id="refLink_9" class="refLink">
      <input type="text" value="/C02E/Java-Notes-Classes1" size="30" name="item.Link" id="Link_9" 
         class="wijmo-wijtextbox ui-widget ui-state-default ui-corner-all">24</input>
   </span>
</td>

I tried to set the value of the input as follows. But it's not working.

var linkValue = 25;
$(".refLink input").html(linkValue);

Can anyone point me to what's wrong?

Upvotes: 1

Views: 249

Answers (4)

Andrew Whitaker
Andrew Whitaker

Reputation: 126042

You want .val not .html:

var linkValue = 25;
$(".refLink input").val(linkValue);

.val is for setting the value of each element (typically used for form fields), and .html is used to set the html content of each element.

Also, per @DavidThomas' comment above, your input should look more like this:

<input type="text" value="/C02E/Java-Notes-Classes1" size="30" name="item.Link" id="Link_9" 
     class="wijmo-wijtextbox ui-widget ui-state-default ui-corner-all" value="24" />

Upvotes: 4

bPratik
bPratik

Reputation: 7019

To begin with:

<input type="text" value="/C02E/Java-Notes-Classes1" size="30" name="item.Link" id="Link_9" class="wijmo-wijtextbox ui-widget ui-state-default ui-corner-all">24</input>

is not standard practice. Input isn't meant to have an end tag and so the value of a textbox is provided via it's value attribute. Thus, the textbox will be set to /C02E/Java-Notes-Classes1 while 24 will just be plain text.

Answering your question, using

$(".refLink input").val(linkValue); 

will allow you to set the value of the textbox, thus essentially over-writting /C02E/Java-Notes-Classes1 rather than 24 which I'm assuming you expect as the behaviour.

Upvotes: 0

Curtis
Curtis

Reputation: 103358

Use .val():

$(".refLink input").val(linkValue);

Upvotes: 0

Colin Brock
Colin Brock

Reputation: 21565

Use .val() instead of .html()

Upvotes: 2

Related Questions