views:

111

answers:

4

Hi All, I have one html structure , somethinh like

 <div>

    <div id="mydiv">
        <h3>
        This is a Test Page
        </h3>
        <div>I am inside a div</div>
        <a href="http://www.google.com"&gt;Click me</a>  
        <div>Second div</div>     
    </div>
</div>

and one script:

 $(function () {
        var elm = document.getElementById("mydiv").innerHTML;// can use html()of jquery
        alert(elm);
    });

Now elm prints/alerts below code:

  <h3>
        This is a Test Page
        </h3>
        <div>I am inside a div</div>
        <a href="http://www.google.com"&gt;Click me</a>  
        <div>Second div</div>  

now my question is what are the ways to get value/access "h3"/"a"

tags from var elm. Note: I want to use elm.[I know this can easily be done by

 $("#mydiv h3")]

so just want to use var elm....the sln can be both in jQuery/javascript.

A: 
$(elm).find("h3")
David Morton
alert($(elm).find("h3").text()); dont work
Wondering
where I am going wrong
Wondering
What's your entire code here? Is elm defined properly? What's the error you're getting?
David Morton
html() is giving 'null' in alert
Wondering
elm is a string according to what you wrote above. You need to change elm to the following:var elm = document.getElementById("myDiv");alert(elm.innerHTML);alert($(elm).find("h3").text());
David Morton
changing code will work, but is there a way to retrive values directly from elm
Wondering
No. Not without accessing it a different way.
David Morton
+2  A: 

Trying to search for the h3 or anchor inside your html string isn't going to work (like the other solutions suggested). You need to change your code so you get the "mydiv" element as a jQuery object and then search inside of it:

var elm = $('#mydiv');
alert(elm.html());
alert(elm.find('h3').text());

Updated

If you really want to load the html in the string and parse it you can do something like the following:

$(function () {
    // This is just a plain string of HTML now
    var html = document.getElementById("mydiv").innerHTML;

    // Add it to a hidden DOM element
    var hidden = $("<div/>").hide().appendTo("body");

    // Add the html, replacing the duplicate ID
    hidden.append(html.replace(/mydiv/, "mydiv2"));

    // Get the values
    alert(hidden.find("h3").text());
    alert(hidden.find("a").text());

    // Destroy holder
    hidden.remove();
});
Lance McNearney
var elm = $('#mydiv'); will work without a doubt, just want to know can i manipulate with elm(which I have), seems its not possible ..any suggestion
Wondering
Please check my updated answer for a solution.
Lance McNearney
will check that, and let u know
Wondering
Worked...KUDOS to u...Cant accept ur ans twice lol!!!..Thnx, really nice idea..
Wondering
+3  A: 

The solution is:

 $(function () {
        var elm = document.getElementById("mydiv");
        alert($(elm).find('h3').text());
    });

I think you wrote

var elm = document.getElementById("mydiv").innerHTML;

See here for a running example.

Vincent
Thnx for all ur help
Wondering
+1  A: 

Vincent and Lance McNearney's answers show how to solve your problem. To answer your exact question, no you cannot get access to "h3"/"a" from your elm element the way you want to. elm is a string based on the html of your and has no ties to the DOM anymore. You could parse it to find the value of the h3 tag (not too elegantly), but you could not tie that tag to the real tag existing in the DOM.

rosscj2533
Exactly...I need to parse that, and it means lot of work :-)..Lance is the first one to point out exact reasons, so I am accepting his ans..Vote up for u :-).. Thnx for all ur suggestion
Wondering