codenijuan codenijuan - 4 months ago 10
Javascript Question

How to get the HTML tags only from a string

Is it possible to get the HTML tag from a string with jQuery (or plain old JavaScript)?

Example:



var htmltag = "<table><tr><td>sample text</td><td><input type="submit" value="Click"/></td></tr></table>";


The output must like this:

<input type="submit" value="Click"/>


How I can get the HTML tag from a string using jQuery?

Answer

You could use a regular expression to match what you want. In this case it might look like this:

htmltag.match("<input .*?>");

The result would be an array containing just the input tag.

Comments