SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Checkbox toggle

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Checkbox toggle

    Can't seem to create a select toggle. When checkbox is checked enable button, when checkbox is unchecked disable button. I need it to work with the for loops though. The first for loops runs the code properly.

    Code JavaScript:
    function selectone () {
    	var message = document.getElementById('pm');
     
    	for (var i = 0; i < message.length; i++)
    	{
    		if(message[i].checked !== checked)
    		{
    			document.getElementById('multiple_action').disabled = true;
    			document.getElementById('drop_button').setAttribute("class", "drop_button disabled");
    		}
    	}
    	for (var i = 0; i < message.length; i++)
    	{
    		if(message[i].checked == checked)
    		{
    			document.getElementById('multiple_action').disabled = false;
    			document.getElementById('drop_button').setAttribute("class", "drop_button");
    		}
    	}
    }

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There are a number of flaws with this. The main one is that getElementById can by its very nature only give you a single DOM element. Therefore a for loop is pointless, because it will be looping over a single element.

    The second flaw is that your two loops are identical, therefore you should combine them into one.

    Finally, you are doing a comparison against a variable called checked, which has not been defined anywhere.

    If #pm was the single checkbox, you could do this:

    Code javascript:
    var chk = document.getElementById('chk').checked;
    document.getElementById('multiple_action').disabled = !chk;
    document.getElementById('drop_button').className = chk ? 'drop_button disabled' : 'drop_button';

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    There are a number of flaws with this. The main one is that getElementById can by its very nature only give you a single DOM element. Therefore a for loop is pointless, because it will be looping over a single element.

    The second flaw is that your two loops are identical, therefore you should combine them into one.

    Finally, you are doing a comparison against a variable called checked, which has not been defined anywhere.

    If #pm was the single checkbox, you could do this:

    Code javascript:
    var chk = document.getElementById('chk').checked;
    document.getElementById('multiple_action').disabled = !chk;
    document.getElementById('drop_button').className = chk ? 'drop_button disabled' : 'drop_button';
    I actually got the function to work using this code. I imagine it still has inefficiencies. I don't know how to combine the loops. When I try to, they break on me.

    function selectone () {
    var message = document.getElementById('pm');

    for (var i = 0; i < message.length; i++)
    {
    if (message[i].checked == false)
    {
    document.getElementById('multiple_action').disabled = true;
    document.getElementById('drop_button').setAttribute("class", "drop_button disabled");
    }
    }
    for (var i = 0; i < message.length; i++)
    {
    if (message[i].checked == true)
    {
    document.getElementById('multiple_action').disabled = false;
    document.getElementById('drop_button').setAttribute("class", "drop_button");
    }
    }
    }

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You haven't addressed the main flaw I pointed out. That's the most important thing. Posting some HTML would help (but use the syntax highlighting).


Bookmarks

Posting Permissions

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