I'm a coding newbie and looking for a way to check in jQuery whether the name of a certain variable corresponds to the ID of a div, and if so fire some code. Unfortunately, something about my syntax is off, and I can't figure out what.
For a simple example, I want to click a div and store the ID of the div as a variable, and if I click it again the variable gets cleared. Simply assigning another boolean variable is not possible due to the rest of the code, I require the ID-check-thingy or something similar.
This is what I have got so far:
<div class="testdiv" id="primary">Bananarama</div>
$(document).ready(function() {
$(".testdiv").click(function() {
if ($(this).attr("id") == "#" + clickedElement) {
// if the ID of the clicked div matches the variable, proceed. This is the part that doesn't seem to work
alert ("Victory"); // display alert so we know everything worked out
var clickedElement = null; // reset the variable
} else {
var clickedElement = $(this).attr("id"); // the div has been clicked, assign its ID to the variable
}
});
});
Aucun commentaire:
Enregistrer un commentaire