Forum Moderators: open

Message Too Old, No Replies

trying to read a div

can read a label but not a div

         

Jaunty Edward

9:38 am on Dec 5, 2008 (gmt 0)

10+ Year Member



Hi, I have wasted hours to do it but I am zero in JS and I really need this thing. Here is this code that shows text between label tag. I need this code to work for div. So that it alerts the code in a Div.


<html>
<body>
<form name ="aaa" action = "">
<label> text1 </label>
</form>
<p onclick="alert(document.getElementsByTagName('label')[0].firstChild.data);">get text</p>
</body>
</html>

I think getElementsByTagName is not the right thing here it should be getElementsById or something but I am not able to fix it, please fix the code.

Regards,
Jaunty

coopster

6:40 pm on Dec 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



An alert pops a message window. To write the code to a div element you create that element and append it to your document. Or have it there already and non-display it, update it upon onclick and show it.

Jaunty Edward

7:36 pm on Dec 5, 2008 (gmt 0)

10+ Year Member



HI,

thanks for the help, but I dont know JS at all, if you could please make the changes in the above code it will be a great help.

Regards,
Jaunty

birdbrain

9:11 pm on Dec 5, 2008 (gmt 0)



Hi there Jaunty_Edward,

try it like this...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<style type="text/css">
#mydiv {
width:500px;
border:3px double #999;
padding:10px;
margin:10px auto;
text-align:justify;
}
#foo {
width:100px;
line-height:30px;
border:3px double #900;
color:#600;
text-align:center;
cursor:pointer;
}
</style>

<script type="text/javascript">
if(window.addEventListener){
window.addEventListener('load',mytest,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',mytest);
}
}

function mytest(){
document.getElementById('foo').onclick=function() {
alert(document.getElementById('mydiv').firstChild.nodeValue)
}
}
</script>

</head>
<body>

<div id="foo" title="click here">get text</div>

<div id="mydiv">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin massa. Nam vehicula.
Morbi velit nisi, mollis id, ultrices luctus, adipiscing sit amet, lectus. Nunc rhoncus
nisl ac enim. Maecenas vestibulum dolor ut velit. Maecenas condimentum pulvinar purus.
Pellentesque ac ipsum. Curabitur sodales, elit vel molestie hendrerit, elit odio rhoncus tellus,
nec gravida enim urna id velit. Donec nec tellus. Vestibulum nulla. Curabitur enim arcu,
ornare id, placerat eget, nonummy vitae, mauris. Nulla rutrum semper odio. Duis vulputate
ornare mauris. Praesent eget nibh sed ante ultricies scelerisque. Duis eget felis ut arcu porta
bibendum. Mauris rutrum. Vivamus consectetuer purus sit amet mi. Suspendisse eu augue.
</div>

</body>
</html>

birdbrain