Possible Duplicate:
How do I select text nodes with jQuery?
I am trying to move the data that is in the text node adjacent to a checkbox on the selection of the checkbox. I am using jquery. The methods I have tried are listed below with the results I received.
<input class="item" type="checkbox" /> Category 1
<input class="item" type="checkbox" /> Category 2
<script>
$('.item').click(function(){
if($(this).attr('checked'))
{
$('#list').append("<span>" + $(this).next() + "<span><br/>")
//$(this).next() just writes "[object Object]"
//$(this).next().text() doesn't write anything
//Also tried $(this).nextSibling.nodeValue but throws "Cannot access property of undefined"
}
else
{
//This is remove the item from the list on uncheck
}
});
</script>
Consider having this markup instead
or this:
Thus you kill two birds with one stone: a) your markup is far semantically better and b) you can access the
labelelements content via the.text()method.The first variant can be seen here: http://jsfiddle.net/skip405/DjWcg/