Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery text Node Object to string

I'm using this code:

/**
 * Get Text Nodes from an Element
 * Returns and object unless number is passed in
 * Numbers is 0 based
 */
(function( $ ) {
$.fn.textNodes = function(number) {
    var nodes = jQuery(this).contents().filter(function(){
        return this.nodeType == 3;
    });
    if(jQuery.isNumber(number)){
        return nodes[number];
    }
    return nodes;
};
})( jQuery );

It's used to grab the text nodes out of html

For example:

<div>
    <span>Testing</span>
    What is this?
</div>

I'm looking for What is this?

This works, as I can do a console.log and see the result in the console.

But when I try and use the result in an input field it give me: [object Text]

How can I use the result as a string value?

I've tried toString() but that gives the same result, unless I missed something.

like image 240
Senica Gonzalez Avatar asked Jan 18 '23 06:01

Senica Gonzalez


1 Answers

Use the nodeValue property:

$("#yourInputField").val(yourTextNode.nodeValue);
like image 96
Frédéric Hamidi Avatar answered Jan 20 '23 16:01

Frédéric Hamidi