#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,670
    Rep Power
    171

    Onclick add value of text box Jquery


    Hi guys;

    I use the code below to add or subtract the value of the text box of number of nights in this link.

    This is my code and it doesnt work. It does call the funciton properly but doesnt update! Any ideas?
    Code:
    $(document).ready(function()
    		{
    			
    $(function()
    {
    	$(".nights_minus").click(function()
    	{
    		var currentVal = parseInt($(this).next(".nights_text_box").val());
    		if (currentVal != NaN)
    		{
    			$(this).next(".nights_text_box").val( Math.max(1,currentVal - 1) );
    		}
    	});
    
    	$(".nights_plus").click(function()
    	{
    		alert(' + Clicked');
    		var currentVal = parseInt($(this).prev(".nights_text_box").val());
    		if (currentVal != NaN)
    		{
    			$(this).prev(".nights_text_box").val(currentVal + 1);
    		}
    	});
    });
    
    });
    Code:
    <table style="border-collapse:collapse; border-spacing: 0;">
                                    <tr>
                                        <td style="text-align:right; padding:0">
                                        	
                                            <img alt="prev" src="images/left_down.png" onClick="showstuff('submit_this'); hidestuff('number_of_results_text');" id="nights_minus" 
                                            class="nights_minus" style="cursor:pointer; margin-right:-3px;margin-top:3px " />
                                        </td>
                                        <td style="padding:0;">
                                        	<input style=" background:none; text-align:center; color:#FFF; font-size:14px;
                                    		 background-image:url(images/center.png); width:36px; height:28px; border:none; background-repeat:none;"
                                    		 onClick="showstuff('submit_this'); hidestuff('number_of_results_text');"  
                                   			 type="text" id="nights_text_box" class="nights_text_box" name="nights" value="3<?php //echo $_GET['nights'];?>" />
                                   		</td>
                                        <td style="text-align:left; padding:0">
                                        	<img alt="next" src="images/right_up.png" onClick="showstuff('submit_this'); hidestuff('number_of_results_text');" 
    		                                 id="nights_plus" class="nights_plus" style="cursor:pointer; margin-left:-2px;margin-top:3px " />
                                     	</td>
                                    </tr>
                                </table>
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,165
    Rep Power
    184
    I think this is what your trying to do, but be aware; that I did modified it a bit:
    Code:
    var newVal;
    
    $(document).ready(function()
    		{
    			
    $(function()
    {
    	$(".nights_minus").click(function()
    	{
    		var currentVal = parseInt($(this).next(".nights_text_box").val());
    		if (currentVal != NaN)
    		{
    			newVal = parseInt($(".nights_text_box").val()) - 1;
                            if (newVal >= 1) {
    			 $(".nights_text_box").val(newVal);
    			}
    			else {
    			 newVal = 1;
    			}
    		}
    	});
    
    	$(".nights_plus").click(function()
    	{
    		var currentVal = $(".nights_text_box").val();
    		if (currentVal != NaN)
    		{
                            newVal = parseInt($(".nights_text_box").val()) + 1;
    			$(".nights_text_box").val(newVal);
    		}
    	});
    });
    
    });

IMN logo majestic logo threadwatch logo seochat tools logo