Your i++ is messing things up. I would have i starting at 0 and move the i++ up to the very start of the function. That way your intention becomes crystal clear.
var i = 0;
$("#addProduct").click(function () {
i++;
...
});
After that, the amount classname doesnât have an index on it and doesnât need one either. You can use â#slider1 .amountâ to target the desired element.
$('#slider' + i + ' .amount').attr(...);
You can help to make the above clearer too, by using a context selector with the id selector as a second parameter within which to search:
A problem still remains tho, add two or more sliders in the fiddle, and see how the value changes on all of them if you drag one slider, do you know how this could be fixed?
That is your slide and change functions causing the problem.
The this keyword in those functions will be the slider itself, such as #slider1 or #slider2, so you can use the same context selector technique as before for this.
As you can see in the fiddle( http://jsfiddle.net/0Lqw0bgx/78/ ), the sliders are created on a click event. However, I would also need these dynamically added sliders on another part of this project. However here i need one in every <div class="slider-outer"></div>
So say if I have two divs with the class=âslider-outerâ I would get this:
Is this a big/hard change to my current code? My newbie jquery brain is thinking something in the lines of: http://jsfiddle.net/0Lqw0bgx/83/ which obviously aint working.
I am trying to get this done, adding it to all divs with a certain class, and i think its starting to work⌠except I got that âvalue changes in all inputs when dragging one sliderâ-issue again, can you see the problem?
It sounds like something is trying to affect a global class name again.
/me checks your code
Yep - look at your slide and change functions.
// bad code
$(this).siblings($(".amount" ).val(ui.value));
What is happening here is that the inner part, the $(".amount" ).val(ui.value) is assigning a value to all classes that match.
Then after that the outer part, $(this).siblings(...); tries to find siblings using bad information.
What you want to do instead, is to get the appropriate sibling, and then set the value
The insertAfter(selector) and insertBefore(selector) methods donât work like the $(selector, context) one.
When you give insertAfter a selector of '.first-text' it will insert that thing after all of those selectors. If you have 10 sliders on the page you will then end up with 100 of the things.
So, giving it a straight selector is not the answer.
Instead of giving it '.first-text' there is another solution. You can give insertAfter a $(selector, context) reference instead, which would look like:
slider.insertAfter($(..., ...));
The same thing has to be done with the insertBefore as well.
You know what the selector is, and I think that you can guess what the context has to be.