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")
1
  • thats because your input tag is inside your label tag Commented May 12, 2015 at 13:49

4 Answers 4

2

You can use:

$taskTemplate.find(".task-manager label").contents().get(0).nodeValue = "M123:";
Sign up to request clarification or add additional context in comments.

1 Comment

Never knew that was possible , but its not easy to understand for beginners :/
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>

5 Comments

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
@DontVoteMeDown Yes why, because it is valid to get input wrapped inside label
@vinodsahu $taskTemplate.find(".task-manager label").contents().get(0).nodeValue = "M123:" would set it
@A.Wolff sure, you're right. I thought it was against the patterns.
But the label won't work properly this way. The label needs a for attribute with the input's id.
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

5 Comments

This doesn't work. The label needs a for attribute with the input's id.
@Perplexor: your suggestion will obviously make it a better code, but OP never had this before either. anyway suggestion taken :)
But this suggestion changes other behaviours on the site. You can't just assume that OP is okay with that while it indeed will resolve his initial issue.
@Perplexor: The First Rule on SO is not to intervene with OP's code which would change the entire behaviour, obviously any good notes can be suggested which could improve the code
And that's exactly why I wrote my comment.
0
$taskTemplate.find(".task-manager label").contents().get(0).nodeValue = "M123:" 

this worked for :)

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.