#EANF#
I am a front-end designer and developer with limited JS and jQuery
experience. I always know I could learn more, but time has never allowed
for that (I could also be using this as an excuse. ; ) Nonetheless, I have
an issue on a website I'm currently developing that I need some JS/jQuery
help on.
https://dl.dropboxusercontent.com/u/63777185/screenshot.png
This image represents subproducts on a product page. When a user opens the
selection box and selects an option, it doesn't automatically check the
checkbox. I was able to find something here on StackOverflow that worked
with minor tweaks, but the problem comes when there is more than one
product. The script that I was using selected all the checkboxes instead
of the unique one that I needed.
Another problem is that this code is dynamically generated, so I don't
control the output. So, I thought the best way to tackle this was to do
addClass to the elements I needed, with an integer to make it unique
(since they need to be unique). From there, I didn't know how to get the
two pieces of code to work together. This is horribly wrong and you're
free to laugh, but this is what I have:
<script>
$(function(){
$('select').each(function(i,j) {
$(this).addClass('selection'+i);
});
$('input').each(function(i,j) {
$(this).addClass('check'+i);
});
$('.selection'+i).change(function() {
var selected = $('select option:selected');
if (selected.val() == "null") {
$('.check'+i).prop('checked', false);
}
else {
$('.check'+i).prop('checked', true);
}
});
});
</script>
Any help is greatly appreciated! : )
No comments:
Post a Comment