0

A simple answer to this question is like this

document.getElementById("form").innerHTML += someHtmlString

And this works too. But my problem is, doing this cause the previous selected field to go to original state (in my case I was using < select > tag and the selected values were restored to original value).

What would be the possible solution. Is there any code that adds/appends in html tags (in my case a div tag) using only JS so that my previously selected options remains the same?

Regard

2 Answers 2

3

Use normal DOM methods. That way the old HTML is never re-interpreted. Sample:

var div = document.createElement('div')
div.innerHTML = '<b>test</b>';
document.getElementById("form").appendChild(div);
Sign up to request clarification or add additional context in comments.

2 Comments

+1, though a reference would probably be helpful ;) developer.mozilla.org/en/…
still gets the page refreshed :(
0

Remember the selected item before appending HTML and after that select the item via JavaScript. Something like this should work:

var index = document.getElementById("idOfSelect").selectedIndex;
document.getElementById("form").innerHTML += someHtmlString;
document.getElementById("idOfSelect").options[index].selected = true;

2 Comments

but I do have a lot of select tags. I am adding select tags dynamically. Select on one option creates another select tags related to the first selected item.
So then, I would use jQuery to handle all of them. But it is not the best solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.