Help using document.getElementById

This is a discussion on "Help using document.getElementById" within the JavaScript Forum section. This forum, and the thread "Help using document.getElementById are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Mar 28th, 2008, 17:06
Up'n'Coming Member
Join Date: Jul 2007
Location: UK
Posts: 97
Thanks: 2
Thanked 0 Times in 0 Posts
Help using document.getElementById

Hi!

I have this piece of javascript which enables some nifty image and text swapping, however I would like it to also include a link around the main image tag but can't get it to work, can anyone tell me what I have to add? I have tried adding a link id and another line to my javascript code but it does not add the link.

Here's the script:

Code: Select all
<script type="text/javascript">
        function replaceMainImage(NEWTEXT, NEWIMAGE) {
             document.getElementById("thecontainer").innerHTML = '<img name="mainimage" src="'+NEWIMAGE+'" alt="Main image">';
            document.getElementById("description").innerHTML = ''+NEWTEXT+'';    
        }
        
</script>
and here is where I use the script in my page:

Code: Select all
<div id="thecontainer">
      
                  <img name="mainimage" src="images/web_main_rennaissance.png" alt="Main image" />
          </div>
This is the thumb image part that, when clicked, replaces "mainimage" with the appropriate image:

Code: Select all
<a href="javascript:void(0);" title="Cow With Horns" onclick="replaceMainImage('Cow With Horny Horns', 'images/photo_main_cowhorns.png');">
                          <img border="0" width="97px" height="76px" 
                          alt="Photo - Cow With Horns" src="images/photo_cowhorns.png">
                      </a>
It was actually someone on these forums who helped me with the code (they wrote it, I used it and thanked them )
Reply With Quote

  #2 (permalink)  
Old Mar 28th, 2008, 20:06
New Member
Join Date: Mar 2008
Location: Space
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help using document.getElementById

<script type="text/javascript">
function replaceMainImage(NEWTEXT, NEWIMAGE, link) {
document.getElementById("thecontainer").innerHTML = '<a href="'+ link +'"><img name="mainimage" src="'+NEWIMAGE+'" alt="Main image"></a?';
document.getElementById("description").innerHTML = ''+NEWTEXT+'';
}

</script>

<a href="javascript:void(0);" title="Cow With Horns" onclick="replaceMainImage('Cow With Horny Horns', 'images/photo_main_cowhorns.png', 'url_you_for_anchor');">
<img border="0" width="97px" height="76px"
alt="Photo - Cow With Horns" src="images/photo_cowhorns.png">
</a>
Reply With Quote
  #3 (permalink)  
Old Mar 28th, 2008, 20:10
New Member
Join Date: Mar 2008
Location: Space
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help using document.getElementById

How the hell do you edit posts in this forum?

I think that is what you wanted.

Just replace 'url_you_for_anchor' (which is how it came out when i tried to type 'url_for_your_anchor'). Each call to the function without a url as an argument will give an invalid number of arguments error, so update all your previous calls to it.
Reply With Quote
  #4 (permalink)  
Old Mar 28th, 2008, 22:52
Up'n'Coming Member
Join Date: Jul 2007
Location: UK
Posts: 97
Thanks: 2
Thanked 0 Times in 0 Posts
Re: Help using document.getElementById

Quote:
Originally Posted by LumberJake View Post
How the hell do you edit posts in this forum?
I have a little button at the bottom of my posts called Edit, it is really faint though.

Quote:
Originally Posted by LumberJake View Post
I think that is what you wanted.
Yes! Thank you very very much! I feel so thick, it looks so obvious, duh! I really ought to be able to work this kind of thing out for myself.. oh well, that's what forums are for.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
document.getElementById and document.getElementByName is not working in FF dhineraj JavaScript Forum 3 Jul 26th, 2007 20:23
document.getElementById("userProfileCheck") is not working in Mozilla Firefox dhineraj JavaScript Forum 1 Jul 20th, 2007 05:32
document.getElementById is not working in Mozilla Firefox dhineraj JavaScript Forum 3 Jul 20th, 2007 02:31
document.getElementById[objectID].style now working on Mozilla Browser yogeshid JavaScript Forum 1 May 25th, 2007 09:30
new problem. document.getElementById().width returning unassigned. why? sanchopansa JavaScript Forum 2 Jul 22nd, 2006 20:23


All times are GMT. The time now is 05:57.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43