Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Plus and Minus jQuery

Posted on 2013-12-04
4
Medium Priority
?
4,847 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:Nathan Riley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 1000 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 44

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 1000 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 12

Author Comment

by:Nathan Riley
ID: 39698582
0
 

Expert Comment

by:gajera bhavin
ID: 41781378
its fack
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
Suggested Courses

636 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