Chris
Chris

Reputation: 442

Extract html as a literal string

If I have a label and its contents are set up like so

<label>
    "Thi"
    <b>
        s i
    </b>
    <b style="font-style: italic;">s a</b>
    <u>
        <b style="font-style: italic;">&nbsp;test </b>
        <i>for </i>
        extracting&nbsp;
    </u>
    stuff
</label>

Which would read, "This is a test for extracting stuff" where some overlapping underlining would be applied as well from "a" to "extracting".

How would I go about extracting the contents of the label into a string that would read like so:

"Thi"<b>s i</b><b style="font-style: italic;">s a</b><u><b style="font-style: italic;">&nbsp;test </b><i>for </i>extracting&nbsp;</u>stuff

So I could then assign that string to a variable?

Upvotes: 0

Views: 150

Answers (1)

realbart
realbart

Reputation: 3965

Don't you simply want

var x = $("label").html();

(providing you have one label) see the jquery documentation for html()

$("label") returns a jquery-object. you can read the html with the html-method. you can also write the html with this method:

$("label").html("<b>the new content</b>");

You can also simply do it without jquery:

var x = document.getElementsByTagName("label")[0].innerHTML;

getElementsByTagName returns an elementcollection. If there's only one label. the first one is the one you want. innerHTML is the propery that contains the HTML.

You might be better of if you give your label an ID.

<label id="myLabel"> 
    ...
</label>

then you could do:

var x = $("#myLabel").html();
var y = document.getElementByID("myLabel").innerHTML;

Upvotes: 1

Related Questions