Subject: Read HTML comments with Javascript
Posted By: benburrows Post Date: 10/29/2004 10:10:15 AM
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

Reply By: Imar Reply Date: 10/29/2004 11:50:34 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
Reply By: richard.york Reply Date: 10/29/2004 11:59:21 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
Reply By: benburrows Reply Date: 10/29/2004 12:50:20 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:-)


Go to topic 21459

Return to index page 729
Return to index page 728
Return to index page 727
Return to index page 726
Return to index page 725
Return to index page 724
Return to index page 723
Return to index page 722
Return to index page 721
Return to index page 720