codenijuan codenijuan - 4 years ago 119
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)?


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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download