Sunday, April 3, 2011

The content is added and showed in the browser but jQuery can't see it.

I have a [WebMethod] that load an html snippet and add some content to it. That [WebMethod] is then injected into a page using $.post() and .html().

A piece of snippet:

<p id="name"></p>

The [WebMethod] add content to it:

<p id="name">Joe</p>

The snippet also contains a JavaScript code, like:

alert($("#id").text())

The problem: $("#id").text() return nothing (like in the snippet) but the content ("Joe") is showed correctly in the browser. Where is the problem?

Thanks and sorry for my EngRish.

From stackoverflow
  • Your element does not have an id of "id", it has an id of "name". Change your selector to this...

    $("#name")
    
  • you should call $("p#name") instead of $("#id");

    <p id="something"> </p>
    $("p#something")...
    
    <p class="something_else"></p>
    $("p.something_else")...
    

0 comments:

Post a Comment