Need a bit of help with JS...
Posted: 22 May 2014 21:30
I need some help with JS. I'm designing a custom rating widget because all of the available jQuery plugins either don't meet the specs or are under shitty license.
Code (mixed with pseudocode):
The problem I'm facing is actually getting the required elements. <div> "x" is not unique on the page (there are many of them), neither are its children ("y") but I must find specific y's in specific x...
With getElementById it would be piss simple, like this it's not and unfortunately I can't give unique id to the x divs (very ugly).
Any help? I'm not very good with JS...
Code (mixed with pseudocode):
Code: Select all
<div class="x">
// ----- before
<span class="y" onclick="func(this)"></span>
<span class="y" onclick="func(this)"></span>
// ----- here we call func (for example)
<span class="y" onclick="func(this)"></span>
// ----- after
<span class="y" onclick="func(this)"></span>
<span class="y" onclick="func(this)"></span>
</div>
<script>
function func(elem) {
if (!elem.hasClass("z")) {
elem.addClass("z");
}
$var belems = elements before called element;
$var aelems = elements after called element;
for belems as elem {
if (elem.hasClass("z")) {
elem.removeClass("z");
}
}
for aelems as elem {
if (!elem.hasClass("z")) {
elem.addClass("z")
}
}
}
</script>
With getElementById it would be piss simple, like this it's not and unfortunately I can't give unique id to the x divs (very ugly).
Any help? I'm not very good with JS...