I have an app that allows user to generate text with HTML code in the following format:
<h2>User generated Dynamic Data 1</h2>
<h3>User generated text 1.1</h3>
<h3>User generated text 1.2</h3>
<h2>User generated Dynamic Data 2</h2>
<h3>User generated text 2.1</h3>
<h3>User generated text 2.2</h3>
<h3>User generated text 2.3</h3>
<h2>User generated Dynamic Data 3</h2>
<h3>User generated text 3.1</h3>
<h3>User generated text 3.2</h3>
This is how it looks like in a browser:
Is there any way to replace what user generated with the one below, using javascript?
<h2>User generated Dynamic Data 1 <button class="something" onclick="bubble_fn_add_headers({output1: 'User generated Dynamic Data 1', output2: 'User generated text 1.1nUser generated text 1.2'});">Save</button></h2>
<h3>User generated text 1.1</h3>
<h3>User generated text 1.2</h3>
<h2>User generated Dynamic Data 2 <button class="something" onclick="bubble_fn_add_headers({output1: 'User generated Dynamic Data 2', output2: 'User generated text 2.1nUser generated text 2.2nUser generated text 2.3'});">Save</button></h2>
<h3>User generated text 2.1</h3>
<h3>User generated text 2.2</h3>
<h3>User generated text 2.3</h3>
<h2>User generated Dynamic Data 3 <button class="something" onclick="bubble_fn_add_headers({output1: 'User generated Dynamic Data 3', output2: 'User generated text 3.1nUser generated text 2.2'});">Save</button></h2>
<h3>User generated text 3.1</h3>
<h3>User generated text 3.2</h3>
This is how the above would look like in a browser:
The situation is very trickey because:
- All the texts surrounded by
<h2></h2>
and<h3></h3>
tags are user generated. - Users can generate any number of
<h2>
Texts followed by any or even zero number of<h3>
texts.
Can you guys suggest any work around this using javascript?
Thanks
I would have tried
s.replace('<h2>', '<h2>User generated Dynamic Data 1 <button class="something" onclick="bubble_fn_add_headers({output1: 'User generated Dynamic Data 1', output2: 'User generated text 1.1nUser generated text 1.2'});">Save</button></h2>')
But it just isn’t possible because the texts are dynamically generated and are unique each time.
4
Answers
You can use regex for this. See the fiddle for more details.
Code::
Don’t use regex or replace to change HTML.
Just use DOM access
Here is the minimum safe way to create the object and embed it in a button
Here is what you asked for. It is VERY BRITTLE and will blow up the first time a user enters a quote or a double quote
Consider generating the HTML elements dynamically from the user input, which is easier and more scalable:
Working example is here.
As for generating more complex/nested elements, better approach would be to store data as JSON object, and render by referencing the keys..
Loop over the headings with
querySelectorAll
One solution would be to iterate over your content, extract the necessary text and generate the buttons dynamically, adding custom event handlers to each button.
If I understood the problem, this should work.
However, I would question a number of things about the system that generated this output in the first place.
One thing is that your HTML seems to have very little structure.
Perhaps each
<h2>
element could be enclosed in a<section>
for example. This would make the task a lot easier because you could loop over the sections and extract the<h2>
and<h3>
elements using simple queries.