0

I am creating div element like below and inserting some html content inside div and innerText.

var creatediv=document.createElement("DIV");
var html="<div align="left"><a id="test" >test</a></div>";
creatediv.innerHTML=html;
creatediv.innerText="testing";

Now my question is how to retrieve the updated html variable here.

Thanks,

Raj

5
  • 1
    As a side note: "<div align="left"><a id="test" >test</a></div>"is not escaped properly. Commented Mar 23, 2011 at 22:18
  • You aren't changing the html variable, so what do you mean by "updated"? And you do realize you are actually creating a div inside of a div, right? Commented Mar 23, 2011 at 22:18
  • @polarblau:Yes your right,Here i want approach to achieve the above scenario. Commented Mar 23, 2011 at 22:19
  • @Travis Webb:Updated means ,after creating html div element,i am inserting html content.Within that html content i want to insert value as testing.After updating value,i want to retrieve html content i inserted before. Commented Mar 23, 2011 at 22:21
  • But you realize that this string will throw a parser error? Commented Mar 23, 2011 at 22:21

4 Answers 4

2

You should be able to retrieve the current HTML from creatediv using creatediv.innerHTML.

var creatediv=document.createElement("DIV");
var html="<div align=\"left\"><a id=\"test\" >test</a></div>";
creatediv.innerHTML=html;
creatediv.innerText="testing";
html = creatediv.innerHTML;
Sign up to request clarification or add additional context in comments.

Comments

0

You just overwritten your innerHTML at line 4. So your innerHTML value in question will be equal to 'testing' string, given your DOM implementation supports non-standard property innerText or equal to html variable value otherwise.

2 Comments

here i dont want only testing,i need entire html div with thw updated value testing.
i dont understand,what is your comment is here.
0

Append the creatediv to dom tree first, then retrieve by function getElementById.

document.append(creatediv);

Comments

0

Well, this is a function that converts value to a string if it's not one. An empty string is returned for null or undefined value:

function baseToString(value) {
    if (typeof value == 'string') {
        return value;
    }
    return value == null ? '' : value + '';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.