Jump to content

[SOLVED] [HELP] With fancy jquery input form


sloth456

Recommended Posts

Hi guys,

 

I'm new to javascript and am exploring jquery as it seems to make things very simple, though I am stuck on this one thing, I hope someone could help me.

 

I have a input form that looks something like this

 

Spare Points: 100

 

Campaign Name  Priority

 

Campaign 1            - 0 +

Campaign 2            - 0 +

Campaign 3            - 0 +

 

I want to use the plus and minus buttons to deduct and add points to each campaign, deducted points get added back to the "spare points", points added to campaigns are taken from the spare points.

 

As I press the plus and minus buttons the numbers need to change accordingly, the total number of points on the screen must only add to 100.

 

I have very little idea where to start, I'm pretty new to jquery.

 

Any ideas?

Link to comment
Share on other sites

some code to start with hope you enjoy learning jQuery as much as I did

 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function() {
    
    $('.minus').click(function() {
        var curr = parseInt($(this).parent().find('.cdisp').html());
        if (curr > 0) {
            $(this).parent().find('.cdisp').html(curr-1);
            $('#spare').html(parseInt($('#spare').html())+1);
        }
    });


    $('.plus').click(function() {
        var curr = parseInt($(this).parent().find('.cdisp').html());
        if (parseInt($('#spare').html()) > 0)  {
            $(this).parent().find('.cdisp').html(curr+1);
            $('#spare').html(parseInt($('#spare').html())-1);

        }
    });

});

</script>

Spare Points: <div id="spare">100</div>
<div>
<div>Campaign Name  Priority</div>

<div class="camp">
Campaign 1             <span class="minus">-</span> <span class="cdisp">0</span> <span class="plus">+</span> 
</div>
<div class="camp">
Campaign 2             <span class="minus">-</span> <span class="cdisp">0</span> <span class="plus">+</span> 
</div>
<div class="camp">
Campaign 3             <span class="minus">-</span> <span class="cdisp">0</span> <span class="plus">+</span> 
</div>

Link to comment
Share on other sites

its pretty simple actually the function names are suggestive to what they do its will really take a long time to explain it over the forum, you can check jquery docs most of the function are there. other than that it plain logic

 

1) minus - minus 1 from current label and adds 1 to spare (before it does this its checks if current label is > 0

2) plus - adds 1 to current label and subtracts 1 from spare (checks if spare points are available)

 

thats its.  as for the jquery

 

 

        var curr = parseInt($(this).parent().find('.cdisp').html());

 

the above finds the elements with class 'cdisp' in the parent so that would be div with class camp and returns the html

 

hope the explanation was good enough I am not too good at explaining stuff :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.