PDA

View Full Version : Javascript assistance



Lee
20-10-2012, 06:00 PM
Hi,

I am trying to learn some JS for a project I'm working on and I have a couple of questions-

getElementById


In the css, If I use .font for example to style a P tag then the getelement by ID works fine however when I use #divone the getelementbyid doesn't work is there a reason for this - also:

do you need to do:

<div class="name" id="divone">

for the getelementbyid to work because I was under the impression the ID was the name of the css (for example .font) the id is font?

Thanks!

hamheyelliot
20-10-2012, 06:07 PM
CSS classes have a dot in front (like .font for example)

Whereas IDs are those that are styled with hashtags (#font)

What ID have you set the getElement to find?

Lee
20-10-2012, 06:13 PM
CSS classes have a dot in front (like .font for example)

Whereas IDs are those that are styled with hashtags (#font)

What ID have you set the getElement to find?

If I have a css class (.myfont) and then use getelementbyid('myfont').style.color = ... I can recolour the text however...

If I have a css id (#mydiv) and then use getelementbyid('mydiv').style.display = ... I can't change the visibility of the div :(


I am working up to a navigation menu using css but I need to start by learning ^ :)

hamheyelliot
20-10-2012, 06:17 PM
If I have a css class (.myfont) and then use getelementbyid('myfont').style.color = ... I can recolour the text however...

If I have a css id (#mydiv) and then use getelementbyid('mydiv').style.display = ... I can't change the visibility of the div :(


I am working up to a navigation menu using css but I need to start by learning ^ :)

Okay, what does your HTML look like? Could be the root of the problem.

Lee
20-10-2012, 06:18 PM
Okay, what does your HTML look like? Could be the root of the problem.

Is there any way we could talk about this using msn (and possibly teamviewer)? :)

Not right now but throughout the next week maybe :P

-Lee

hamheyelliot
20-10-2012, 06:24 PM
Is there any way we could talk about this using msn (and possibly teamviewer)? :)

Not right now but throughout the next week maybe :P

-Lee

Yeah, my MSN is [email protected] if you want to add me on there. :) Would probably be easier to just see the entirety of the code after all.

Agnostic Bear
28-10-2012, 07:49 PM
You aren't setting the id attribute correctly. If you were it would work.

element
#id
.class

<div id="theid" class="theclass">

would be:
element = document.getElementsByTagName('div')
id = document.getElementById('theid')
class = document.getElementsByClassName('theclass')

You don't include # in the id tag on the actual html. If you are doing so, remove it, if you're not doing that and you use my exact bit of html and javascript and it's still broken, re-install your browser as it is broken.

Want to hide these adverts? Register an account for free!