#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    33
    Rep Power
    2

    Any suggestions with this-> javascript code


    Code:
    var 1 = document.getElementById("1").click();
    var 2 = document.getElementById("2").click();
    var 3 = document.getElementById("3").click();
     if(1){
        userChoice =="1";
    }else if (2){
        userChoice == "2";
    }else if (3){
        userChoice == "3";
    }
    I'm working on code that gets user choice when the user clicks on a div.I'm attempting to 'get' the element and assign it to a variable but it returns null, couldn't find a solution in jquery either.Any suggestions??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    876
    Rep Power
    276
    assuming this is your exact code, you CANNOT START a VARIABLE or ID or CLASS using a NUMBER.

    read up on your basics. that is a part of webdev-101.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    33
    Rep Power
    2
    Originally Posted by DonR
    assuming this is your exact code, you CANNOT START a VARIABLE or ID or CLASS using a NUMBER.

    read up on your basics. that is a part of webdev-101.
    oh, i didn't want to paste my exact code so i used numbers instead , duh, i've never actually done that in any working code
    Lets try this
    Code:
    var cat = document.getElementById("cat").click();
    var dog = document.getElementById("dog").click();
    var snake = document.getElementById("snake").click();
     if(cat){
    	userChoice =="cat";
    }else if (dog){
    	userChoice == "dogr";
    }else if (snake){
    	userChoice == "snake";
    }
    the reason i want the clicks to output a string is , i want the string to be passed in a comparison function like this
    function compare(Userchoice,ComputerChoice);
    which is sronger.
    The computer choice is derived by assigning strings to 3 Math.random() ranges.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by rhodoscoder
    oh, i didn't want to paste my exact code so i used numbers instead
    If you want us to help you debug your code, we need the real, actual code, not some fantasy code you made up. If you give us fantasy input, you'll only get fantasy results.

    So is this now the real code you're actually running in your browser?

    A lot of this still doesn't make sense. Calling the click() method actually clicks on the element. It also doesn't have a return value, as far as I can tell. And you seem to confuse the equality operator == with the assignment operator =.

    OK, I think the issue here is that you've never checked whether the functions you're using actually work like you think they work. If you're not sure what a function does, look it up or try it out.

    I guess what you meant is something like this:

    javascript Code:
    // define event handler
    var handleChoice = function (event) {
        var choice = event.target.id;
        alert("The user chose: " + choice);
    };
     
    // attach handler to "click" event
    document.getElementById("cat").addEventListener('click', handleChoice, false);
    document.getElementById("dog").addEventListener('click', handleChoice, false);
    document.getElementById("snake").addEventListener('click', handleChoice, false);
    The 6 worst sins of security How to (properly) access a MySQL database with PHP

    Why cant I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    33
    Rep Power
    2
    @Jacques1 I'm curious how many programming languages are you experienced in and how did you go about learning them.
    I'm trying to learn JS and php , I think i know the basics, how do I go about being proficient at them and then develop applications?? I meddle around but clearly I'm not well versed.
    As always thank you.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    33
    Rep Power
    2
    I get this error with the chrome developer tool
    Uncaught TypeError: Cannot call method 'addEventListener' of null .So the little that i read on addEventListener , means it fires when the user interacts with a DOM element in this case you passed in click ,but i don't get the bubbling and capturing, i'm i not tying to capture the click event here??
  12. #7
  13. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,009
    Rep Power
    2791
    Code, rhodos, code. I'm guessing that you are not giving an accurate reference for the listener.
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.

IMN logo majestic logo threadwatch logo seochat tools logo