Skip to content Skip to sidebar Skip to footer

Jquery Change Text Between Two Elements

I want to change text between two elements using JQuery, but I don't have any idea ! for example: This text must be changed !!!

Solution 1:

You can call contents() on the parent element to obtain its child text nodes, then use slice() with index() to locate the text nodes you want to remove. From there on, after() will allow you to add the new content:

var ch1 = $("input:checkbox[name=ch1]"),
    ch2 = $("input:checkbox[name=ch2]"),
    contents = ch1.parent().contents();
contents.slice(contents.index(ch1) + 1, contents.index(ch2)).remove();
ch1.after("The text was changed.");

You can test it in this fiddle.

Solution 2:

You use js replace function to substitute the text you want to replace Here is the example: http://jsfiddle.net/yangchenyun/8zFFc/

Here is the code

var replacedText = $('input[name=ch1]').parent().html().replace('This text must change !!!', 'replaced text');

$('input[name=ch1]').parent().html(replacedText);

Solution 3:

Are the 2 inputs in a bigger container div? Then you could se-set that divs innerHTML without the text inbetween

Solution 4:

Can you ad a span like this?

<input type='checkbox' name='ch1'>
<span id="change"> This text must change !!!</span>
<input type='checkbox' name='ch2'>

Solution 5:

this might be helpful for you:

http://api.jquery.com/nextUntil/

Post a Comment for "Jquery Change Text Between Two Elements"