p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   Read HTML comments with Javascript (http://p2p.wrox.com/showthread.php?t=20598)

benburrows October 29th, 2004 10:10 AM

Read HTML comments with Javascript
 
Does anyone know how to read HTML comments with javascript?
ie. I have a section in my HTML like
<div id="test">
<!--
Some words
-->
</div>

I want to get "some words" into a javascript variable.
I need this to be cross browser compatable.
I have read books and looked on the net and I think what I want is possable but I cannot make it work.
Thanks
Ben


Imar October 29th, 2004 11:50 AM

Hi Ben,

You can use the innerHTML property of the <div> tag like this:

alert(document.getElementById('test').innerHTML);

You'll need to do some string parsing to get rid of the comment tags, but that shouldn't be too hard.

HtH,

Imar

richard.york October 29th, 2004 11:59 AM

If you just want to set aside some text, it might be easier to use the CSS display property.

<div id='test' style='display: none;'>
    Some words.
</div>

<script type='text/javascript'>
    // Then just access the contents of that div like this
    document.getElementById('test').innerHTML;
</script>

The text in the <div> isn't visible in CSS capable browsers.

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail

benburrows October 29th, 2004 12:50 PM

Imar,
Thanks for the sugestion, works a treat :-)
Can't beleive I did not try that, I got distracted by trying to directly get at the comment tag itself.

Richard,
Thanks for the Sugestion. Unfortunately I can't just "hide" the text, I need it ignored completely until I want to use it.


Thanks:-)



All times are GMT -4. The time now is 08:29 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.