Gabriella said:
OK, thanks, so if I get the textarea's value through value attribute,
not innerHTML -
still - is there a way to extract HTML tags and create DOM elements
from this string?
Using regexp and functions like match() & indexOf() will make it very
very hard to find the DOM element's attributes.
If you are letting a user enter HTML into a text area, you can:
1. Create a DOM element (a div is likely best) then insert the text
using innerHTML. The browser's HTML parser will then create DOM
elements inside the div and you can use normal DOM manipulation on the
created elements. If there are any typos or errors in the HTML,
results may vary from browser to browser depending on error correction.
It might be best to use an "in-page popup" to show the user the
results of their input and allow them an opportunity to modify it. (see
below).
2. Create your own HTML parser in JavaScript, which I would not
recommend at all.
Below is a simple 'preview' example, the elPos() function is
simplistic, it will work in most cases but not all.
<title>Preview play</title>
<script type="text/javascript">
function previewPop(txt, posObj){
// Variables
var wPort; // Container for preview "window"
var wBar; // Menu bar for wPort
var wClose; // Close text to close wPort
var wCont; // Use to display txt as innerHTMl
// Create outer container
wPort = document.createElement('div');
wPort.style.border = '2px solid #ACACAC';
wPort.style.borderStyle = 'outset';
wPort.style.width = '75%';
wPort.style.zIndex = '10';
wPort.style.backgroundColor = '#ffffff';
if (posObj && 'number' == typeof posObj.top
&& 'number' == typeof posObj.left){
wPort.style.position = 'absolute';
wPort.style.top = posObj.top + 'px';
wPort.style.left = posObj.left + 'px';
}
// Create a top menu bar
wBar = document.createElement('div');
wPort.appendChild(wBar);
wBar.style.backgroundColor = '#D3EDFA';
wBar.style.borderBottom = '1px solid #ACACAC';
wBar.style.textAlign = 'right';
// Add a close button (pseudo-link with onclick)
wClose = document.createElement('span');
wClose.appendChild(document.createTextNode('Close'));
wClose.onclick = (function(el){
return function(){
el.parentNode.removeChild(el);
}
})(wPort);
wClose.style.textDecoration = 'underline';
wClose.style.cursor = 'pointer';
wClose.style.paddingRight = '10px';
wClose.style.color = 'blue';
wBar.appendChild(wClose);
// Create a div and assign text to its innerHTML property
wCont = document.createElement('div');
wPort.appendChild(wCont);
wCont.innerHTML = txt;
document.body.appendChild(wPort);
}
// Return the position of an element on the page - simple version
function elPos(el){
var pos = {top:0, left:0};
while(el.offsetParent){
pos.top += el.offsetTop;
pos.left += el.offsetLeft;
el = el.offsetParent
}
return pos;
}
</script>
<form action="">
<div>
<input type="button" value="Preview"
onclick="previewPop(this.form.ta.value, elPos(this.form.ta))">
<textarea name="ta" rows="20" cols="50">
<pre>
Here is some text
</pre>
<b>And here is some more bolded</b><br>
<img src="g.gif" width="100px" height="150px">
</textarea>
</div>
</form>