Check if the clicked div id matches with variable

為{幸葍}努か 提交于 2020-01-06 21:02:49

问题


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
        }
    });
});

回答1:


You can use directly compare your variable with this.id

if(this.id == clickedElement)

However I would recommend you to use .is()

if($(this).is('#' + clickedElement))

$(document).ready(function() {
  var clickedElement;
  $(".testdiv").click(function() {
    if (this.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
      clickedElement = null; // reset the variable
    } else {
      clickedElement = this.id; // the div has been clicked, assign its ID to the variable
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="testdiv" id="primary">Bananarama</div>



回答2:


You have 2 issues in your code:

1) You don't need extra ID selector in if comparison here. .attr("id") returns ID of element and not selector for it.

2) You don't need to re-declare variable inside if and else condition. due to redefining the variable in those conditions are restricting its scope inside that particluar condition only.

Also you should minimize recreation of jquery object for target element. Its always a good practice to create it once and then re use it further:

var $this = $(this);
var clickedElement;
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
        clickedElement = null;     // reset the variable
} else {
        clickedElement = $this.attr("id");     // the div has been clicked, assign its ID to the variable
}


来源:https://stackoverflow.com/questions/41165653/check-if-the-clicked-div-id-matches-with-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!