1

I have HTML like following :

<div class="task-manager">
    <label>Manager: <input type="text" value="" /></label>
</div>

I need to modify text of label Manager dynamically. I tried using JQUERY text method, but it replaces input type also.

$taskTemplate.find(".task-manager label").text("M123")
DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
vinod sahu
  • 67
  • 12

4 Answers4

2

You can use:

$taskTemplate.find(".task-manager label").contents().get(0).nodeValue = "M123:";
DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
1

You should change your HTML code, because <input> field is inside <label> and when you are changing value of this label, it's also overwriting and removing input form field:

<div class="task-manager">
    <label>Manager:</label> <input type="text" value="" />
</div>
Jazi
  • 6,569
  • 13
  • 60
  • 92
  • Is it not possible without changing HTML. I am able to get value using $taskTemplate.find(".task-manager label").contents().get(0).nodeValue, but can not modify it. thanks for help – vinod sahu May 12 '15 at 13:53
  • @DontVoteMeDown Yes why, because it is valid to get input wrapped inside label – A. Wolff May 12 '15 at 13:54
  • 1
    @vinodsahu $taskTemplate.find(".task-manager label").contents().get(0).nodeValue = "M123:" would set it – A. Wolff May 12 '15 at 13:54
  • @A.Wolff sure, [you're right](http://stackoverflow.com/q/774054/1267304). I thought it was against the patterns. – DontVoteMeDown May 12 '15 at 13:58
  • 1
    But the label won't work properly this way. The `label` needs a `for` attribute with the `input`'s id. – Christian Lundahl May 12 '15 at 14:04
1

Just move the Input tag outside the label tag, because when your updating the text of your label, its erasing the content of label (which will obviously remove input tag inside it) , so change your code like this

HTML code:

<div class="task-manager">
   <label for="title">Manager:</label>
   <input type="text" id = 'title' value="" />
</div>

JS code:

    $('.task-manager label').text('Deputy Manager :');

Live Demo @ Jsfiddle: http://jsfiddle.net/dreamweiver/w6arp71x/

note/suggestion:for attribute added to label to bind the input to the label by default

halfer
  • 19,824
  • 17
  • 99
  • 186
dreamweiver
  • 6,002
  • 2
  • 24
  • 39
0
$taskTemplate.find(".task-manager label").contents().get(0).nodeValue = "M123:" 

this worked for :)

vinod sahu
  • 67
  • 12