57

I have next html:

<label for="user_name">
  <abbr title="required">*</abbr>
  Name
</label>

And I want to change label caption to Title with jquery. So I do

$('label[for=user_name]').html('Title')

And it replaces all inner html (including abbr tag)

So, what's the easiest way to replace only Name?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
mikdiet
  • 9,859
  • 8
  • 59
  • 68

8 Answers8

84

If you use contents() method it will also return text nodes. Since jQuery doesn't have text node methods, convert last node to a DOM node

$('label[for="user_name"]').contents().last()[0].textContent='Title';

Demo: http://jsfiddle.net/yPAST/1/

charlietfl
  • 170,828
  • 13
  • 121
  • 150
50

Sorry for the late reply... But here is a way to do so using only jQuery:

$('label').contents().last().replaceWith('Title');
Dom
  • 38,906
  • 12
  • 52
  • 81
  • 1
    You saved the day. Thanks :) – Kushagra Gour Dec 14 '14 at 20:48
  • 3
    Note that a string to `replaceWith` is treated like HTML; if it contains tags it will be parsed to DOM elements. – holmis83 Jan 15 '16 at 12:05
  • These answers are good, so +1, but its brittle to navigate the DOM like this. When using jQuery it's a good idea to avoid .first(), .last(), .prev(), .next(), etc. – png Jun 19 '18 at 22:33
3

It may not be the prettiest way, but this works:

var $label = $('label[for=user_name]');
$label.html($label.html().replace("Name", "Title"));
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
1

Evans solution added to jquery fn to make it's use comfortable:

// get/change node content not children
jQuery.fn.content = function( n ){
    var o = $(this).clone();
    var c = o.children().remove();
    if (typeof n === "string" ){
        o.html(n);
        $(this).html(c).append(n);
    }
    return o.html();
}

Usage :$('myselector').content('NewContentString');

Erik255
  • 1,463
  • 1
  • 11
  • 13
1

This is the solution that worked for the most browsers

$('label[for="user_name"]').contents().last()[0].nodeValue = 'Title';

This one came close but gave issues in ie8 since textContent is not supported

$('label[for="user_name"]').contents().last()[0].textContent='Title';
khr055
  • 28,690
  • 16
  • 36
  • 48
1

You can select only the abbr element, store it, and then replace the whole content with the stored element plus the changed caption:

​$('label[for="user_name"]').each(function(){
   var a = $(this).children('abbr');
   $(this).html(a).append('Title');
});

See this fiddle

m90
  • 11,434
  • 13
  • 62
  • 112
1

you can use replace accomplish this

  var html = $('label[for=user_name]').html().replace('Name','Testing');
  $('label[for=user_name]').html(html);

check it : http://jsfiddle.net/DyzMJ/

Jignesh Rajput
  • 3,538
  • 30
  • 50
0

if you are manipulating more than 1 label you can select each label and replace text with jquery:

$('label[for="user_name"]').contents().last().replaceWith("Title");

and for the second label :

$('label[for="user_lastname"]').contents().last().replaceWith("Title2");

and so on ...

Seif Tml
  • 2,413
  • 1
  • 17
  • 15