1

Suppose I have a code like that

<p>hello</p>

I want to assign the above code to a variable, how to do that?

3
  • what do you want to achieve exactly , do you want to store in a variable and append it later... Commented Dec 3, 2010 at 7:00
  • Sorry that my question is not clear, I just found the solution by using .html, thank you Commented Dec 3, 2010 at 7:18
  • basically you want html from dom , we have jquery .html which does that Commented Dec 3, 2010 at 7:18

2 Answers 2

1

var content = $('p').html();

Ah dammit, too late.

If you want a specific paragraph you should give it an id and use:

var content = $('p#id').html();

instead.

Sign up to request clarification or add additional context in comments.

1 Comment

How to do the reverse process? Assign again the var content contents to p. For example you needed to do a backup of p, modify it and recover the previous value.
0
var a = "<p>hello</p>";

6 Comments

@srinivas , what will the above code get if you have two p elements in the page
he said assign that code to a variable not anything else so what does it matter if he has hundreds of them?
who and what? :P now you lost me
@breezer there was some code from someone called...srinivas which says $('p').html , i left the above comment for him...somehow it came for you
@gov it will give the first p data.After realizing that i deleted my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.