It's known that Opera & Safari use value property rather than innerHTML to read teaxtarea content. How I can retrieve innerHTML of div with nested textarea? I created simple code, which works fine in IE, but not other browsers. Thank you.
Already did. html() retrievs null, text() - empty string. I resolved the issue modifying textarea to the following state:
<textareaid="MyTextArea"rows="5"cols="30"onchange="this.innerHTML=this.value;">InitialText</textarea>
Not the best solution but it works in all browsers.
You can access content of given div as DOM child nodes. In this case have to be make difference between types of nodes; text is of type 3 and can be read by nodeValue property, while Textarea - by innerHTML, as it is shown in example:
<script language="javascript" type="text/javascript">
function getContent() {
var s="";
var myDiv = document.getElementById('myDiv');
for(n=0;n<myDiv.childNodes.length;n++)
{
if(myDiv.childNodes[n].nodeType==3)s+=myDiv.childNodes[n].nodeValue;
else s+=myDiv.childNodes[n].innerHTML;
}
alert(s);
}
</script>
<div id="myDiv">
Text Before TextArea
<br />
<textarea id="MyTextArea" rows="5" cols="30">InitialText</textarea><br />
Text After TextArea
</div>
vicpeters
Member
22 Points
9 Posts
innerHTML of div having TextArea
Feb 16, 2012 04:42 PM|LINK
It's known that Opera & Safari use value property rather than innerHTML to read teaxtarea content. How I can retrieve innerHTML of div with nested textarea? I created simple code, which works fine in IE, but not other browsers. Thank you.
<script language="javascript" type="text/javascript">
function getContent() {
var myDiv = document.getElementById('myDiv');
alert(myDiv.innerHTML);
}
</script>
<div id="myDiv">
Text Before TextArea
<br />
<textarea id="MyTextArea" rows="5" cols="30">InitialText</textarea><br />
Text After TextArea
</div>
<br />
<input type="button" onclick="getContent();" value="Test" />
MetalAsp.Net
All-Star
112241 Points
18268 Posts
Moderator
Re: innerHTML of div having TextArea
Feb 16, 2012 05:08 PM|LINK
Use the magic of jQuery.
vicpeters
Member
22 Points
9 Posts
Re: innerHTML of div having TextArea
Feb 16, 2012 05:34 PM|LINK
I tried jQuery. Could you please to give me example?
srinanthuram
Contributor
6800 Points
1549 Posts
Re: innerHTML of div having TextArea
Feb 16, 2012 06:13 PM|LINK
hi
pls see this url
http://www.davidtong.me/innerhtml-innertext-textcontent-html-and-text/
thank u
vicpeters
Member
22 Points
9 Posts
Re: innerHTML of div having TextArea
Feb 16, 2012 06:22 PM|LINK
Already did. html() retrievs null, text() - empty string. I resolved the issue modifying textarea to the following state:
<textarea id="MyTextArea" rows="5" cols="30" onchange="this.innerHTML=this.value;">InitialText</textarea> Not the best solution but it works in all browsers.
srinanthuram
Contributor
6800 Points
1549 Posts
Re: innerHTML of div having TextArea
Feb 16, 2012 06:45 PM|LINK
k
see this url
http://stackoverflow.com/questions/3676927/why-if-element-innerhtml-is-not-working-in-firefox
http://stackoverflow.com/questions/1642447/how-to-change-the-content-of-a-textarea-with-javascript
thank u
tom22
Member
246 Points
70 Posts
Re: innerHTML of div having TextArea
Feb 16, 2012 07:07 PM|LINK
instead of "innerHTML" , you try to get the chil elements.
or try to get the text area by its id directly.
Zhelezov
Member
278 Points
65 Posts
Re: innerHTML of div having TextArea
Feb 16, 2012 08:20 PM|LINK
You can access content of given div as DOM child nodes. In this case have to be make difference between types of nodes; text is of type 3 and can be read by nodeValue property, while Textarea - by innerHTML, as it is shown in example:
<script language="javascript" type="text/javascript">
function getContent() {
var s="";
var myDiv = document.getElementById('myDiv');
for(n=0;n<myDiv.childNodes.length;n++)
{
if(myDiv.childNodes[n].nodeType==3)s+=myDiv.childNodes[n].nodeValue;
else s+=myDiv.childNodes[n].innerHTML;
}
alert(s);
}
</script>
<div id="myDiv">
Text Before TextArea
<br />
<textarea id="MyTextArea" rows="5" cols="30">InitialText</textarea><br />
Text After TextArea
</div>
O Zhelezov
vicpeters
Member
22 Points
9 Posts
Re: innerHTML of div having TextArea
Feb 17, 2012 02:41 PM|LINK
Good idea! Only thing the function should be modified the following way:
function getContent() {
var s = '';var value='';
var myDiv = document.getElementById('myDiv');
for(n=0; n<myDiv.childNodes.length; n++)
{
if (myDiv.childNodes[n].nodeType == 3) {
value = myDiv.childNodes[n].nodeValue;
if (value != undefined)
s += value;
}
else {
value = myDiv.childNodes[n].value;
if (value != undefined)
s += value;
}
}
alert(s);
}
Otherwise it still does not work on Safari