November 9th, 2013, 03:47 PM
-
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??
November 9th, 2013, 04:20 PM
-
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.
November 9th, 2013, 04:46 PM
-
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.
November 9th, 2013, 05:15 PM
-
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);
November 9th, 2013, 06:25 PM
-
@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.
November 9th, 2013, 06:36 PM
-
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??
November 9th, 2013, 07:28 PM
-
Code, rhodos, code. I'm guessing that you are not giving an accurate reference for the listener.