Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8490659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:11:25+00:00 2026-06-10T22:11:25+00:00

I have the following Code var page = ‘<h1>Hello!</h1><p>FOO</p><span class=username>this is ur name</span><p>sample text</p>’;

  • 0

I have the following Code

var page = '<h1>Hello!</h1><p>FOO</p><span class="username">this is ur name</span><p>sample text</p>';

when I alert $(page).html() i get Hello! and when I wrap the contents of the page in a div and alert $(page).html() I get the whole html contents.

What I am trying to accomplish here is I have a page string with html template and I am trying to find a class username in it and i am getting null.

I am confused with this happens

Here is a small fiddle of the issue

http://jsfiddle.net/6TSuq/1/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:11:26+00:00Added an answer on June 10, 2026 at 10:11 pm

    When you call $(page) you construct a jQuery object which contains 4 elements; for each of the HTMLElements that aren’t nested in your string.

    console.log($(page).map(function () {
       return this.nodeName;  
    }).toArray());
    
    // ["H1", "P", "SPAN", "P"] 
    

    ​
    html() returns the innerHTML of the first element in this jQuery object; which is why you only see “Hello!”.

    To find the .username you should use the filter() method, which searches within the jQuery object for elements which match the given selector.

    alert($(page).filter('.username').text()); // "this is ur name", kthx.
    

    See your updated fiddle here; http://jsfiddle.net/6TSuq/15/

    Bare in mind that in future, you might have nested elements such as this;

    var page = "<h1><span class='username'>Foo</span></h1>";
    

    In this circumstance, filter()ing for .username will yeild no results; as the jQuery object $(page) does not contain the .username element; it contains a h1, which has a descendant which is a .username. Therefore here you’d need to use;

    alert($(page).find('.username').text());
    

    For reference, see find(), filter() and map()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code snippet in my page. <script type=text/javascript> function expandCollapse(id){ var
I have the following code in may page var x = parseInt($(this).css('backgroundPosition').split( )[0]); Everything
I have the following code in a CSHTML razor page: @{ var sort =
I have the following (simplified) code: var Foo = (function () { var data
I have the following code on my page: $('#Report').click(Report); function Report() { var e
I have the following code: <script type=text/javascript> var checksSinceLastPostBack = new Array(); function clientSelectedIndexChanged(sender,
I have the following code: class Parameterizable{ var map: Map[String, String] = new scala.collection.immutable.HashMap()
I have the following snippet of a HTML select box: <select id=Obj_cboscreen class=menubarselect name=page>
I have the following jQuery code in place on my page: var isChanged =
I have the following code which posts file to ASP.net page: using (var Client

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.