Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
-2
rated 0 times [  2] [ 4]  / answers: 1 / hits: 27365  / 12 Years ago, thu, december 6, 2012, 12:00:00

I am passing a DOM object after an onclick event occurs and I need to know the id of the element I've passed from the html. I've read about the this.id that exists in JavaScript but I cannot seem to utilize it correctly.



Here is my code in the function:



function clickFunction() {
$(.tictactoe).on('click', function() {

$(this).text(X);
if (this.id == upleft) {
$('#upright').text(yes);
}
});
}

$(document).ready(function() {
$('#playagain').hide();
$('.tictactoe').on(click);

clickFunction();

});


So I thought that my quick test, if successful, would present the word yes in the grid of a tic tac to bar in the upper right corner when I clicked the position on the left.



Perhaps there is something wrong with my comparison?



this.id == upright


The X's show up, indicating the rest of the code works, so it must be the comparison. Let me know your thoughts.


More From » jquery

 Answers
24

Pass the clickFunction in the click handler. Then remove the unnecessary click handler in the function.



$(document).ready(function(){
$('#playagain').hide();
$('.tictactoe td').on(click, clickFunction); //Added td click handler
});

function clickFunction()
{
$(this).text(X);
if(this.id == upleft)
{
$('#upright').text(yes);
}
}

[#81568] Wednesday, December 5, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jacquezf

Total Points: 27
Total Questions: 109
Total Answers: 98

Location: Lesotho
Member since Wed, Jun 2, 2021
3 Years ago
;