I'm trying to change the opacity of a with javascript when a div is clicked.
Here is my html:
<section id="section2">
<img onclick="one()" class=img src="1.png" />
</section>
<section id="6-1">
Hello World
</section>
And my javascript:
function one() {
document.getElementById('6-1').style.opacity = '1';
document.getElementById('section2').style.opacity = '0';
}
For some reason, its not working
Thanks!
I'm trying to change the opacity of a with javascript when a div is clicked.
Here is my html:
<section id="section2">
<img onclick="one()" class=img src="1.png" />
</section>
<section id="6-1">
Hello World
</section>
And my javascript:
function one() {
document.getElementById('6-1').style.opacity = '1';
document.getElementById('section2').style.opacity = '0';
}
For some reason, its not working
Thanks!
Share Improve this question asked Dec 8, 2012 at 22:32 Thomas LaiThomas Lai 3171 gold badge6 silver badges16 bronze badges 4- 1 What browser are you testing in? – Thierry Blais Commented Dec 8, 2012 at 22:33
- 1 And what does your css look like? – Thierry Blais Commented Dec 8, 2012 at 22:34
- 3 ID's starting with numbers are invalid, other than that it works just fine -> FIDDLE – adeneo Commented Dec 8, 2012 at 22:46
- When using opacity, you should try to look for cross-browser patibility. So, IE 9+ and most modern browsers use opacity=1, IE8- uses filter='alpha(opacity=100)', others use -moz-opacity=1 and -khtml-opacity=1. – user1694691 Commented Dec 8, 2012 at 23:38
2 Answers
Reset to default 2Try to set the value without the ' ', like this:
document.getElementById("section2").style.opacity = 1;
It worked for me just fine.
Like ModernDesigner said, enquote your attributes and change your ids to not start with numbers, but other than that, I still couldn't figure out your problem. But for now you can use jQuery to make it work.
Here's a Fiddle that toggles it every time you click it, so hope this helps
It kept saying the function wasn't defined, so maybe you had some incorrect syntax or something. For now though i used jQuery's .click()
function instead of onClick=
because it wasn't working.