Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing HTML (using JavaScript)

Status
Not open for further replies.

flanakin

Programmer
Mar 21, 2000
158
US
I have a section of code that I'd like to surround with table tags. In order to make the page more readable and make the code more re-usable, I want to make a function to add in the code for the begin and end table tags. The only way I've found out how to do this is the following:<br><br>&lt;a name=me&gt;<br>All my HTML<br>&lt;/a&gt;<br>...<br>&lt;script lang=javascript&gt;<br>document.all.me.innerHTML = '&lt;h1&gt;' + document.all.me.innerHTML + '&lt;/h1&gt;'<br>&lt;/script&gt;<br><br>That would come out with the text being formatted as a header 1 would be formatted. Well, that would work fine, but on certain occasions, the user may want to have an anchor in that section. When you place an anchor tag w/in another anchor tag, it just kicks out all the rest of the code. So, the window displays fine, but is ended when the first &lt;a&gt; tag (w/in the outer &lt;a&gt;) is reached.<br><br>Is there any way I can get around this? Any help is appreciated. Thanks in advance. <p>Cid<br><a href=mailto:ciddivine@yahoo.com>ciddivine@yahoo.com</a><br><a href= > </a><br>
 
I would use a div and give it an id like this:<br><font color=red>&lt;div id=&quot;bob&quot;&gt;&lt;/div&gt;</font><br>Then you can use script to put whatever you want inside of it.<br><br><font color=red>bob.innerHTML=&quot;some html...including anchors&quot;</font><br><br><A HREF="mailto:jared@aauser.com">jared@aauser.com</A>
 
DIVs don't work. I've tried them. <p>Cid<br><a href=mailto:ciddivine@yahoo.com>ciddivine@yahoo.com</a><br><a href= > </a><br>
 
Shot in the dark: span? :eek:) not much different than div, but I'm not sure why div didn't work so it was worth a shot. Personally, I don't see how this makes your code more reusable, as I think the innerHtml is out of any future version of JavaScript making this code usable for a few months (less than that if you want Netscape 6 preview users to be able to view your pages).<br><br>I'd do this with flat file databases being accessed from Perl in a CGI script. Maybe set it up as a hashtable. But JavaScript for page display is not usually a wise choice, because the chances of multiple people seeing your page as you intended it to be seen is slim to none.<br><br>If you decide go with JavaScript, please let me know what it is about div (or span, or whichever) that's not working with your code. Also, let me know how many different browsers (and flavors of browsers) you want this to work in. If you decide to go with a more robust solution that will work on any browser and be reusable for more than 6 months (but requires a bit more work), I'll see what I can do there too.<br><br>In any case, best of luck. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
&lt;SPAN&gt; comes up as undefined (just like &lt;DIV&gt;s)<br><br>All my users will be using IE, so that's my primary focus, but, if I could find a way to do this which would be more compatible with other browsers, I would be more than happy to do so. I honestly have never seen the innerHTML property before and still have yet to see any documentation on it. It was suggested to me by an associate, so, seeing as tho it was the only thing around, I used it.<br><br>The best thing would be some sort of function/procedure that took in some variables so that the code could be formatted in a virtually pleasing manor, and then just spit out the code.<br><br>If there's a way to do this on the client (not the server) that would be reusable, then that would be perfect. There cannot be too much server interaction, however. Any help is appreciated. Thanks. <p>Cid<br><a href=mailto:ciddivine@yahoo.com>ciddivine@yahoo.com</a><br><a href= > </a><br>
 
If you are using IE5+, then you have access to ALL the HTML content of a DIV or a SPAN, and can rewrite it dynamically anytime you want. In fact, you have access to the innerHTML, outerHTML, innerText, and outerText properties of just about any tag that has an ID (not a &quot;name=&quot;). I know because my project is using these feature extensively. There must be some problem with your code.<br><br>And, no, I don't see innerHTML going away from IE anytime soon. Its a cornerstone of having a true Document Object Model. The syntax and methods will be different with Netscape 6, but you will be able to accomplish the same things.<br><br>I'm not sure I understand your need here: are you saying you want to dynamically be able to change the HTML tags around your main block of HTML, based on user choices, or you just want them to appear when your page loads? If it's the latter, just use document.write().
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top