CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Resolved Won't append to the array (http://www.codingforums.com/showthread.php?t=274713)

akzes 09-29-2012 06:05 PM

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>


Philip M 09-29-2012 06:16 PM

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.

xelawho 09-29-2012 06:19 PM

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 :rolleyes:

akzes 09-29-2012 07:44 PM

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

AndrewGSW 09-29-2012 07:56 PM

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.

Philip M 09-29-2012 08:06 PM

Quote:

Originally Posted by akzes (Post 1274831)
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.

xelawho 09-29-2012 08:27 PM

Quote:

Originally Posted by akzes (Post 1274831)
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)?

akzes 09-29-2012 09:18 PM

Quote:

Originally Posted by xelawho (Post 1274803)
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 :rolleyes:

Quote:

Originally Posted by xelawho (Post 1274847)
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.

akzes 09-29-2012 09:27 PM

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!

xelawho 09-29-2012 09:52 PM

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>



All times are GMT +1. The time now is 12:41 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.