0

How can we change the text data from except span text?

<h2 id="nameUser" >Muhammed <span> mobile :989 531 9991</span></h2>

Is there any solution to change h2 except span?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Mo.
  • 26,306
  • 36
  • 159
  • 225

7 Answers7

4

.contents() returns a collection of nodes, including text nodes. So in your case this would work:

$('#nameUser').contents()​​​​​​​​[0].nodeValue = 'Another name';​​

If you want to get every node except the SPAN, try:

$('#nameUser').contents().filter(function() {
    return this.nodeName != 'SPAN';
}).​each(function(i) {
    // modify each text node
    this.nodeValue = 'name '+i;
});​​​​​

DEMO: http://jsfiddle.net/Vks82/

David Hellsing
  • 106,495
  • 44
  • 176
  • 212
  • This one is good because it will retain the order of things inside the h2 tag if there are span children before and after the text to be modified. Where as most of the other examples do not. http://jsbin.com/ayedub/4/edit – Anthony Hatzopoulos Nov 07 '12 at 14:34
2

Search for the first textnode in childNodes of the h2 element. Change the value of the textnode.

var element = document.getElementById('nameUser');
element.childNodes[0].nodeValue = 'New String';

..should work. Only for this example, because the first childnode is the textnode you want, you don't have to search for it. Otherwise you do..

Jochem Kuijpers
  • 1,770
  • 3
  • 17
  • 34
  • @maanu I wasn't aware of this issue, but IE counts childNodes differently from other browsers (firefox). Does this help you? http://www.webdeveloper.com/forum/showthread.php?156010-childNodes-in-Firefox-vs-IE&s=d295e3e1c2ff528c94360787920ae41e&p=782524#post782524 , alternatively you might want to use gdoron's method, if it suits you better and you're fine with using jQuery for this particular problem. – Jochem Kuijpers Nov 08 '12 at 14:48
1
$('#nameUser').contents().each(function() {
    if (this.nodeType == 3) 
        this.data = "The text you want here";
});​

Live DEMO

gdoron
  • 147,333
  • 58
  • 291
  • 367
1

This example may help you to change father element without changing child elements:

var content= $('#nameUser').children();
$('#nameUser').text('Altered Text').append(content);​
halilb
  • 4,055
  • 1
  • 23
  • 31
1

You can do it by saving the children first here is a codepen of it working.

http://codepen.io/beckje01/pen/sGLot

var h2 = $('#nameUser');
var elmsToSave = h2.children();

h2.empty();
h2.text('bob');
h2.append(elmsToSave);

As pointed out in the comments this will only work if the text to change is first.

Jeff Beck
  • 3,944
  • 3
  • 28
  • 45
0

This will work not only for span but also for any other element which you want the text without the text of it's children.

$("#nameUser")
    .clone()   
    .children()
    .remove()  
    .end()  
    .text();

For change the text I've create a simple jQuery function:

replaceTextWith = function($element, $replacement){
    $oldText = $element.clone().children().remove().end().text();
    $element.text($element.text().replace($oldText, $replacement));
}

replaceTextWith($("#nameUser"), "Bruno"); //usage

Here's a live example working on fiddle

Bruno Vieira
  • 3,884
  • 1
  • 23
  • 35
0

try this ...

<h2 id="nameUser" >Muhammed <span id="nameUserSpan"> mobile :989 531 9991</span></h2>

<script>
$(document).ready(function() {
    var inspan= $("#nameUserSpan").html();
    var newuser='New User';
    $("#nameUser").html('');
    $("#nameUser").html(newuser + '<span id="nameUserSpan">' + inspan + '</span>');
});
</script>
edparry
  • 688
  • 1
  • 10
  • 34
Dario
  • 417
  • 4
  • 17