Solved

Plus and Minus jQuery

Posted on 2013-12-04
4
3,575 Views
Last Modified: 2016-09-02
I've found this example and it's exactly what I need for my site.

http://jsfiddle.net/puJ6G/389/

The only issue is that I need it several times on the same page.  If I click the plus sign then it increases all the instances of it on the page not just the one.  I've tried adding an ID in different places but still no luck.
0
Comment
Question by:N R
4 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 250 total points
ID: 39697517
Hi,

here an adjusted sample:
http://jsfiddle.net/EE_RainerJ/ZzYHt/

Basicly I use the IDs of the fields and rewrote the jQuery selectors:
Script
jQuery(document).ready(function(){
    // This button will increment the value
    $('.qtyplus').click(function(e){
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get its current value
        var currentVal = parseInt($('#'+fieldName).val());
        // If is not undefined
        if (!isNaN(currentVal)) {
            // Increment
            $('#'+fieldName).val(currentVal + 1);
        } else {
            // Otherwise put a 0 there
           $('#'+fieldName).val(0);
        }
    });
    // This button will decrement the value till 0
    $(".qtyminus").click(function(e) {
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get its current value
        var currentVal = parseInt($('#'+fieldName).val());
        // If it isn't undefined or its greater than 0
        if (!isNaN(currentVal) && currentVal > 0) {
            // Decrement one
            $('#'+fieldName).val(currentVal - 1);
        } else {
            // Otherwise put a 0 there
            $('#'+fieldName).val(0);
        }
    });
});

Open in new window


HTML:
<form id='myform' method='POST' action='#'>
    <input type='button' value='-' class='qtyminus' field='quantity1' />
    <input type='text' id='quantity1' value='0' class='qty' />
    <input type='button' value='+' class='qtyplus' field='quantity1' /><br/>
    <input type='button' value='-' class='qtyminus' field='quantity2' />
    <input type='text' id='quantity2' value='4' class='qty' />
    <input type='button' value='+' class='qtyplus' field='quantity2' /><br/>
    <input type='button' value='-' class='qtyminus' field='quantity3' />
    <input type='text' id='quantity3' value='99' class='qty' />
    <input type='button' value='+' class='qtyplus' field='quantity3' /><br/>
</form>

Open in new window


HTH
Rainer
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 total points
ID: 39697787
Couple of things wrong with the suggested code - 'field' is not a valid attribute so I wouldn't advise using it. Also, there are  is no 'name' attribute to your inputs so if you ever need to submit your form - the values won't be submitted.

Have a look at this:

<form id='myform' method='POST' action='#'>
    <input type='button' value='-' class='qtyminus' />
    <input type='text' name='quantity[]' value='0' />
    <input type='button' value='+' class='qtyplus' />
    <br/>
    <input type='button' value='-' class='qtyminus' />
    <input type='text' name='quantity[]' value='0' />
    <input type='button' value='+' class='qtyplus' />
    <br/>
    <input type='button' value='-' class='qtyminus' />
    <input type='text' name='quantity[]' value='0' />
    <input type='button' value='+' class='qtyplus' />
</form>

Open in new window

$('.qtyminus').click(function() {
    qty = $(this).next('input');
    var currentVal = parseInt(qty.val());
    var newVal = (!isNaN(currentVal) && currentVal > 0) ? currentVal - 1 : 0;
    qty.val(newVal);
});

$('.qtyplus').click(function() {
    qty = $(this).prev('input');
    var currentVal = parseInt(qty.val());
    var newVal = (!isNaN(currentVal)) ? currentVal + 1 : 0;
    qty.val(newVal);
});

Open in new window

http://jsfiddle.net/ChrisStanyon/5gdeh/
0
 
LVL 11

Author Comment

by:N R
ID: 39698582
0
 

Expert Comment

by:gajera bhavin
ID: 41781378
its fack
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now