Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Assigning a Variable to an input name

    I have the following:

    Code:
    var $inputs = $('input.cb-element');
    but i would like to update the cb-element to cb-element1, cb-element2 etc depending on what the user clicks. For now i would like to know if i could create a variable and concatinate it to cb-element.

    i tried:

    Code:
    var number = '1';
    
    var $inputs = $('input.cb-element') + '$number');
    but this is not working and i can't seem to find how to do it?

    Can anyone help me?

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    It doesn't work because you're trying to append the number to the returned result and not the actual selector string use;
    Code:
    $('inputs.cb-element'+number);
    Also for reference you do not need to quote integers when declaring them, even if you're going to concat it to a string as JavaScript is a dynamic language.

  • #3
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Iszak for the response, with your assistance, I'm so close to getting this working i can almost smell it! One small step for mankind, on giant leap for my non coding brain.

    I now have this, i can get the id of the checkbox selected and display that in an alert but i am getting a page error with number undefined when i comment out the hard coded number variable. I think i understand thats it's not acessible to the other function. Is it possible to make this work?

    Again, thanks for your help!

    Code:
    $(document).ready(function() {
       	
    	$("input:checkbox").click(function() {
         	var number = $(this).attr("id");
    		alert("id="+number);
       });
     });
    	
    $(function(){
       
    	//var number = 1;
    	
    	var $inputs = $('input.cb-element'+number), $checkall = $('input.checkAll'+number);
        
        $checkall.live('change', function(){
            $inputs.attr('checked', this.checked ? 'checked' : '');
            $(this).next().text( this.checked ? 'Uncheck All' : 'Check All');
        });
        
        $inputs.live('change', function(){
            $inputs.length === $inputs.find(':checked').length 
                ? $checkall.attr('checked', 'checked').next().text('Uncheck All') 
                : $checkall.removeAttr('checked').next().text( 'Check All' );
        });
    });

  • #4
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok so now i have this and it seems to be working perfectly in Firefox and Opera, but in IE i have to select a checkbox then deselect it and then select it again and then it works and for Safari and chrome i have to do that for every section.

    Code:
    $(document).ready(function() {
    
       	$("input:checkbox").click(function() {
         	
    		number = $(this).attr("id");
    		//alert("id="+number);
    		var $inputs = $('input.cb-element'+number), $checkall = $('input.checkAll'+number);
    		
    		$checkall.live('change', function(){
            	$inputs.attr('checked', this.checked ? 'checked' : '');
            	$(this).next().text( this.checked ? 'Uncheck All' : 'Check All');
    		});
    		
    		$inputs.live('change', function(){
            $inputs.length === $inputs.find(':checked').length 
                ? $checkall.attr('checked', 'checked').next().text('Uncheck All') 
                : $checkall.removeAttr('checked').next().text( 'Check All' );
        	});
       });
     });
    Does anyone know what is causing this?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •