codenijuan codenijuan - 2 months ago 6
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?


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.