SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need to find node text with jQuery

    <h1>November<span>2009</span></h1>

    I need help making a variable equal the h1 html() without the span text.

    // equals 'November2009'
    var monthDelete = $('h1').html();

    // I need just 'November'

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    $('h1')[0].firstChild.data;

    A more robust solution:

    Code JavaScript:
    // A plugin:
    jQuery.fn.directInnerText = function() {
        return this[0] && jQuery.map(this[0].childNodes, function(n){
            return n.nodeType === 3 ? n.data : [];
        }).join('');
    };
     
    // Usage:
    jQuery('h1').directInnerText(); // => "November"
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •