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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Won't append to the array

    Hi Everyone! First time post and enjoying javascript. However, I can't seem to figure out how to append to the array and for the if statement to check if the variable got pushed into the array.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    var chosen=[];
    var feat=function(chosenfeat){
    	if (chosenfeat==="den"){
    		if (chosen.indexOf("den")){
    			document.getElementById('denid').checked=false;
    			alert("Are you sure you don't want a Den? They are free!");
    			chosen.splice(chosen.indexOf("den",1))}
    		else {
    			chosen.push=("den");
    			alert("Congradulations! You recieve a complimentary free den!");
    			console.log(chosen);}
    		
    		}
    		}
    </script>
    </head>
    <body>
    <input type="checkbox" id="denid"  onclick="feat('den');" />Den (NO CHARGE)<br />
    </body>
    </html>
    Last edited by akzes; 09-29-2012 at 09:27 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    chosen.push("den");

    alert("Congradulations! You recieve a complimentary free den!"); // spelling errors



    Quizmaster: In the Bible, Joseph and Mary rode into Bethlehem on what sort of animal?
    Contestant: A reindeer.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,912
    Thanks
    56
    Thanked 545 Times in 542 Posts
    not sure that I get it entirely, but I think this is closer...
    Code:
    <script type="text/javascript">
    var chosen=[];
    var feat=function(chosenfeat,box){
    	if (chosenfeat==="den"){
    		if (!box.checked){
    			document.getElementById('denid').checked=false;
    			alert("Are you sure you don't want a Den? They are free!");
    			chosen.splice(chosen.indexOf("den"),1)
    			console.log(chosen);
    			}
    		else {
    			chosen.push("den");
    			alert("Congradulations! You recieve a complimentary free den!");
    			console.log(chosen);}
    		
    		}
    		}
    </script>
    </head>
    <body>
    <input type="checkbox" id="denid"  onclick="feat('den',this);" />Den (NO CHARGE)<br />
    </body>
    although note that IE<9 doesn't support indexOf for arrays

  • #4
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Phillip! That really cleared up a HUGE error.

    So I got it working, except its putting 'den' into the array twice, it should only go in once its checked off, and when it becomes unchecked, should remove 'den' from the array.

    Any suggestions?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    var chosen=[];
    var feat=function(chosenfeat){
    	if (chosenfeat==="den"){
    		if (chosen.indexOf("den",1)===1){ //CHANGED IT RIGHT HERE!
    			document.getElementById('denid').checked=false;
    			alert("Are you sure you don't want a Den? They are free!");
    			chosen.splice(chosen.indexOf("den",1))
    			console.log(chosen);}
    		else {
    			chosen.push("den");
    			alert("Congradulations! You recieve a complimentary free den!");
    			console.log(chosen);}
    		
    		}
    		}
    </script>
    </head>
    <body>
    <input type="checkbox" id="denid"  onclick="feat('den');" />Den (NO CHARGE)<br />
    </body>
    </html>
    They taught us in school that indexOf is ok...I guess not lol. No one uses IE9 :P ahaha

  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    You can still use with IE<9 if you add the following to the top of your code:

    Code:
    if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function(elt /*, from*/) {
            var len = this.length;
            var from = Number(arguments[1]) || 0;
            from = (from < 0) ? Math.ceil(from) : Math.floor(from);
            if (from < 0)
                from += len;
            for (; from < len; from++) {
                if (from in this && this[from] === elt)
                    return from;
            }
            return -1;
        };
    }
    You should copy this EXACTLY as it is. I believe it's from Mozilla.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by akzes View Post
    So I got it working, except its putting 'den' into the array twice
    Well, as I see it they get a free Den (whatever that is) whether they want it or not.

    alert("Are you sure you don't want a Den? They are free!");
    chosen.splice(chosen.indexOf("den",1))

    Surely you want confirm box where the user can select OK or cancel.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,912
    Thanks
    56
    Thanked 545 Times in 542 Posts
    Quote Originally Posted by akzes View Post
    it should only go in once its checked off, and when it becomes unchecked, should remove 'den' from the array.
    wouldn't it make sense, therefore, to check if the box has been checked (see my code at post #3)?

  • #8
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    not sure that I get it entirely, but I think this is closer...
    Code:
    <script type="text/javascript">
    var chosen=[];
    var feat=function(chosenfeat,box){
    	if (chosenfeat==="den"){
    		if (!box.checked){
    			document.getElementById('denid').checked=false;
    			alert("Are you sure you don't want a Den? They are free!");
    			chosen.splice(chosen.indexOf("den"),1)
    			console.log(chosen);
    			}
    		else {
    			chosen.push("den");
    			alert("Congradulations! You recieve a complimentary free den!");
    			console.log(chosen);}
    		
    		}
    		}
    </script>
    </head>
    <body>
    <input type="checkbox" id="denid"  onclick="feat('den',this);" />Den (NO CHARGE)<br />
    </body>
    although note that IE<9 doesn't support indexOf for arrays
    Quote Originally Posted by xelawho View Post
    wouldn't it make sense, therefore, to check if the box has been checked (see my code at post #3)?
    Ummm yes, I just don't get how you did it. I see that you added an extra variable to bring into the function, but it doesn't at all relate to the checkbox name or anything!

    You brought 'this' into the function 'feat' under the variable 'box.' how does box know that its related to that checkbox?

    But however, I still want to look into the array if 'den' is there.
    Last edited by akzes; 09-29-2012 at 09:20 PM.

  • #9
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    var chosen=[];
    var feat=function(chosenfeat){
    	if (chosenfeat==="den"){
    		if (chosen.indexOf("den")===0){ //fixed this!
    			document.getElementById('denid').checked=false;
    			alert("Are you sure you don't want a Den? They are free!");
    			chosen.splice(chosen.indexOf("den")) //fixed this!
    			console.log(chosen);}
    		else {
    			chosen.push("den");
    			alert("Congradulations! You recieve a complimentary free den!");
    			console.log(chosen);}
    		
    		}
    		}
    </script>
    </head>
    <body>
    <input type="checkbox" id="denid"  onclick="feat('den');" />Den (NO CHARGE)<br />
    </body>
    </html>
    Alright! I figured it out! THANK GOODNESS! Here is the final code, Enjoy!

  • #10
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,912
    Thanks
    56
    Thanked 545 Times in 542 Posts
    well, almost. Here's how to make it work in IE8 and below...

    Code:
    <script type="text/javascript">
    if (!Array.prototype.indexOf) { 
        Array.prototype.indexOf = function(obj, start) {
             for (var i = (start || 0), j = this.length; i < j; i++) {
                 if (this[i] === obj) { return i; }
             }
             return -1;
        }
    }
    
    var chosen=[];
    var feat=function(chosenfeat){
    	if (chosenfeat==="den"){
    		if (chosen.indexOf("den")===0){ //fixed this!
    			alert("Are you sure you don't want a Den? They are free!");
    			chosen.splice(chosen.indexOf("den"),1) //the second argument is how many elements to remove - required
    			console.log(chosen);
    						}
    		else {
    			chosen.push("den");
    			alert("Congradulations! You recieve a complimentary free den!");
    			console.log(chosen);
    			}
    		}
    	}
    </script>


  •  

    Posting Permissions

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