PDA

View Full Version : page into variables



midhul
08-09-2008, 04:31 PM
hi,
i posted about this topic a few days ago! I found how to do most of it!
but i did not get this part.
i need a javascript code that can read the html code from the textarea and
take all the elements and their attributes into variables.
for example.
there is an element text box name text1 with size and color attributes.
so the script should make a variable called text1_size=<the size>
and another text1_color=<the color>

note: It should take all the variables of the element, as many as they are. and make variables like those in the above example.

Thanks! in advance! Hope somebody has a solution!

jscheuer1
08-09-2008, 05:53 PM
I had to do something like that for this:

http://home.comcast.net/~jscheuer1/side/dom_b.htm

I forget how I did it though. You are welcome to look over the code for any insights:

http://home.comcast.net/~jscheuer1/side/DOM_b_input.js

If you have any questions, they may jog my memory. The code is annotated in two posts starting here:

http://www.dynamicdrive.com/forums/showthread.php?p=52536&highlight=annotated+version#post52536

midhul
08-10-2008, 06:27 AM
i only want it to directly read html code from textarea.
and take. all the elements and their attributes into variables

jscheuer1
08-10-2008, 07:00 AM
i only want it to directly read html code from textarea.
and take. all the elements and their attributes into variables

There is no HTML code in textarea, only text. So, if I may take literally what you are saying, you don't need to read attributes. You need to parse the text. You know what I mean?

jscheuer1
08-10-2008, 07:36 AM
You want to do something like:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function parseAttys(el){
var v = document.getElementById(el).value,
m = v.match(/<[^ \/]+ [^\/]+>/g);
for(var i = 0; i < m.length; ++i)
m[i] = (i? '\n' : '') + m[i].match(/<([^ ]+)/)[1] + '_' + i + ' attributes:\n' + m[i].replace(/"/g, '').match(/ ([^ ]+=[^ >]+)/g).join('\n');
return m.join('\n');
}
</script>
</head>
<body>
<textarea id="ta" cols="50" rows="5"><span id="bob">Hey!</span><p align="center" width=100>Yo</p></textarea><br>
<input type="button" value="Go" onclick="alert(parseAttys('ta'));">
</body>
</html>

Right? I mean this isn't a solution, but it should be a step along the the way to one. What do you think?