Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 29th, 2004, 10:10 AM
Registered User
Join Date: Jan 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

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.

Old October 29th, 2004, 11:50 AM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Hi Ben,

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


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


Old October 29th, 2004, 11:59 AM
richard.york's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts

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.

<script type='text/javascript'>
    // Then just access the contents of that div like this

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


[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
Old October 29th, 2004, 12:50 PM
Registered User
Join Date: Jan 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts

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.

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read javaScript variables's values in brows AccessAasif@aol.com C# 0 October 15th, 2007 10:21 AM
javascript calendar won't appear unless HTML = out crmpicco Javascript How-To 1 May 9th, 2007 05:48 AM
VBA Formatting cells with Html tags read from XML hemagiri Excel VBA 0 November 22nd, 2006 02:41 AM
can't read value from select tag using javascript kumar_kumar Javascript 2 February 9th, 2005 11:18 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.