croppio.com
croppio.com

Reputation: 1883

Change the text from child nodes

I have the following markup:

<label class="radio">
<input class="radio" type="radio" name="month_type" value="0" checked="checked">
On day 5
</label>

and i need to change the text from 'On day 5' to 'On day 6'. The selection needs to be upward from input to label.

I have tried:

$("input[name='month_type']").parent().text(''); // the result is an empty string
$("input[name='month_type']").parent().contents().filter(function(){
return this.nodeType === 3; // Uncaught SyntaxError: Unexpected token ILLEGAL 
})​.remove();​

with no luck.

Upvotes: 0

Views: 87

Answers (4)

Mehran Hatami
Mehran Hatami

Reputation: 12961

to change the text you'd better try javascript itself instead of jQuery. you might know your label has 2 childNode. the first one is nodType = 1 the last one nodeType = 3. try this and you'll get what you want:

$("input[name='month_type']").parent()[0].lastChild.textContent = "On day 6";

Upvotes: 0

renuka
renuka

Reputation: 549

Try this

$("#textchange").click(function(){
$('input[name=month_type]').parent().html('<input class="radio" type="radio" name="month_type" value="0" checked="checked">On day 6');
});

http://jsfiddle.net/4j37C/

Upvotes: 1

Musa
Musa

Reputation: 97672

Try

$("input[name='month_type']")[0].nextSibling.data = ' On day 6'; 

http://jsfiddle.net/W3WLD/1/

Upvotes: 0

Aditya Singh
Aditya Singh

Reputation: 9612

Try this out:- http://jsfiddle.net/adiioo7/4s2eV

JS:-

$("input[name='month_type']").parent().html(function(){
    return $(this).html().replace("On day 5","On day 6");
});

Upvotes: 1

Related Questions