Simple DHTML
- Started
- Last post
- 11 Responses
- Dancer
I would like to change the BG colour of an H2 heading on click of it's self.
I know this is simple and it should be something like (dunno if the code will show):
< h2 >< a href= " OnClick = "this.background-color="red";"> Heading < / h2 >
Any help would be grand
- neverblink0
onclick="this.backgroundColor='r...
In javascript all attributes with a dash become camelstyle
(background-color -> backgroundColor)
- uan0
should be this.style.backgroundColor
- Dancer0
So, next question... how would I target the containing div instead?
something like: OnClick="getElementByID("wrapper...
- Dancer0
OnClick="getElementByID(" wrapper " ). style . BackgroundColor = 'red ' ; "
- uan0
document. getElementById('wrapper') . style . backgroundColor
make sure the wrapper has the id="wrapper" attribute.
- Dancer0
Sorry one more uan...
I have just realised that this is a class. Would I target that by GetElementbyClass(" whatever ")
- uan0
just add the id="wrapper" to your tag.
there is no getElementbyClass.
- Dancer0
Thanks for all your help uan...
- neverblink0
another way would be to use 'parent' (assuming the wrapper div is the parent of the h2, and the a is a child of the h2)
this.parent.parent- Thanks nb... do you think this would be a better way of doing it?Dancer
- it might be a tad quicker (not noticeable), but getElementById is more general and can be used in almost any situationneverblink
- any situation..neverblink