Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Need help creating elements and removing them
Old 02-06-2009, 03:49 PM Need help creating elements and removing them
Novice Talker

Posts: 8
Trades: 0
Hello everyone! This is my first time at webmaster-talk, I was referred by someone in web_design.reddit, for some basic javascript help.

I'm trying to learn the ins and outs of javascript and I'm making a website where I have some 800x250 images that I want to splash the center of the screen when you click the thumbnail and close again when the splash is clicked.

However, I am having some trouble with creating elements, appending them, and removing them. Here is my script:

PHP Code:
            var bigImg null
            var 
parent document.getElementById("content"); 
 
            function 
makeBig(source) { 
                
bigImg document.createElement('img'); 
                
parent.appendChild(bigImg); 
                
bigImg.style.position="absolute"
                
bigImg.style.left=((winWidth() - 800)/2) + "px"
                
bigImg.style.top=((winHeight() - 250)/2) + "px"
                
bigImg.src=source
                
bigImg.id="bigImg"
                
bigImg.onclick=killBig(); 
            } 
 
            function 
killBig() { 
                
parent.removeChild(bigImg); 
                
bigImg null
            } 
 
            function 
showBig(source) { 
                if (
bigImg !== null) { 
                    
killBig() 
                } 
                
makeBig(source); 
            } 
And when I try it, I get this error: "parent is null" which lists this line as the offender:

PHP Code:
parent.removeChild(bigImg); 
inside the killBig() function.

All help is greatly appreciated! Thanks.
mnemonik is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-06-2009, 03:57 PM Re: Need help creating elements and removing them
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Move the script from in the document head to between the closing </body> and the closing </html>

It will work then.

Then see if you can tell us why.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-07-2009, 09:34 PM Re: Need help creating elements and removing them
Novice Talker

Posts: 8
Trades: 0
Quote:
Originally Posted by chrishirst View Post
Move the script from in the document head to between the closing </body> and the closing </html>

It will work then.

Then see if you can tell us why.
Hmmmmmmmm, I moved the script and it isn't working still... I gave the bigImg a z-index of 5 so it should be on top of everything else. However, I'm not getting any errors in the console now. What difference does it make if the script is between the closing body and html tags?
mnemonik is offline
Reply With Quote
View Public Profile
 
Old 02-08-2009, 07:01 AM Re: Need help creating elements and removing them
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
What difference does it make if the script is between the closing body and html tags
You are supposed to be working out why it doesn't cause errors.

But it is because the element "content" now exists when the script runs.

We need a URL so we can see ALL the code.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-08-2009, 01:48 PM Re: Need help creating elements and removing them
Novice Talker

Posts: 8
Trades: 0
Quote:
Originally Posted by chrishirst View Post
You are supposed to be working out why it doesn't cause errors.

But it is because the element &quot;content&quot; now exists when the script runs.

We need a URL so we can see ALL the code.
http://dl.getdropbox.com/u/413030/Portfolio/index.html Thanks, it helps me a lot to have some feedback to get past the learning curve. I'm trying to get a job as "web content editor" at my school and I think it's mostly writing, but you have to have web skills too. Thanks.
mnemonik is offline
Reply With Quote
View Public Profile
 
Old 02-08-2009, 08:41 PM Re: Need help creating elements and removing them
Novice Talker

Posts: 8
Trades: 0
Nevermind. I just made the images load normally but with display:none and used javascript to bring them forward when needed. Thanks for the help.
mnemonik is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need help creating elements and removing them
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.47339 seconds with 12 queries