user2232273
user2232273

Reputation: 4964

Replace HTML between two tags

What I want to do is replace what is in between two HTML tags.

I'm using this as a reference but I'm still encountering problems: REFERENCE

This is what I've tried:

el.getValue().replace(/<form.+<\/form>/, "<div></div>");

I need to replace all my form tags dynamically.

Upvotes: 1

Views: 1553

Answers (2)

user2232273
user2232273

Reputation: 4964

Yeah. I found a solution.

el.getValue().replace(/<form[\s\S]*?<\/form>/, "<div></div>");

Explanation by @[James G]: [\s\S]*? means [any character including space and line breaks]any number of times, and the ? makes the asterisk "not greedy," so it will stop (more quickly) when it finds </form>.

Reference

Upvotes: 0

Valentin Coudert
Valentin Coudert

Reputation: 1919

If you use jQuery, just retrieve the parent element of what you'd like to be replaced, and replace the content with the .html() function.

Ex:

var formParentElement = $('#formParentElement');
formParentElement.html("<div>my new content</div>");

If you don't use jQuery:

var formParentElement = document.getElementById("formParentElement");
formParentElement.innerHTML = "<div>my new content</div>";

The example assumes the parent element of your form has an ID with value "formParentElement".

Upvotes: 1

Related Questions