Solved

Plus and Minus jQuery

Posted on 2013-12-04
4
4,160 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery form 9 44
Code Coverage and Coding Standards 2 52
Update the value of table B with value of table A tables inner join 6 78
SP result not being displayed 5 47
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

696 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