- This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
Viewing 3 posts - 1 through 3 (of 3 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › Loop instead of loads of if / else statements
I’m using the following javascript (jquery) code at the moment:
<
p>`if($h1p.mouseIsOver()) { $h1p.prev().css("background-color", $fullercolor_bg); } else { $h1p.prev().css("background-color", ""); } if($h2p.mouseIsOver()) { $h2p.prev().css("background-color", $fullercolor_bg); } else { $h2p.prev().css("background-color", ""); } if($h3p.mouseIsOver()) { $h3p.prev().css("background-color", $fullercolor_bg); } else { $h3p.prev().css("background-color", ""); }</p></p>
<pre><code> etc`
I know there is some method which would make this easier and faster, like saying use h1,h2,h3,h4,h5,h6 and then loop through but I’ve been looking around and can’t find any examples I can work it out from.
Could anyone help?
If only CSS had a < parent selector! (I know it’s coming soon)
I found this plugin: https://github.com/Idered/cssParentSelector which as I understand the CSS4 spec should work with:
!h4 + p
but unfortunately it only works with the child selector >
e.g. !p > a
Just in case anyone else has the same problem in years to come: http://stackoverflow.com/questions/18103979/alternative-to-multiple-if-else-statements-in-javascript/18104064#18104064