简体   繁体   中英

jQuery element is not changing text color when clicked

My code is not changing the text color of the label element to #ffffff . No errors are logged.

JS Code

$(document).ready(() => {
  $('.label').on("click", () => {
    $(this).css("color", "#ffffff")
  })
})

HTML Code

<div id="q1">
  <img src="lizerds/l2.jpeg" class="img"/>
        <div>
          <div class="radioWrapper">
            <label class="label">
              <input type="radio" id="q1Snake" class="radio" name="q1radio">
              Snake
            </label>
          </div>
          <div class="radioWrapper">
            <label class="label">
              <input type="radio" id="q1Lizard" class="radio" name="q1radio">
              Legless Lizard
            </label>
          </div>
        </div>
      </div>

You can not use this in an anonymous function to refer to the local instance. Change the arrow function to a "function" and your code will work as expected:

 $(document).ready(() => { $('.label').on("click", function() { $(this).css("color", "red") }) })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="q1"> <img src="lizerds/l2.jpeg" class="img"/> <div> <div class="radioWrapper"> <label class="label"> <input type="radio" id="q1Snake" class="radio" name="q1radio"> Snake </label> </div> <div class="radioWrapper"> <label class="label"> <input type="radio" id="q1Lizard" class="radio" name="q1radio"> Legless Lizard </label> </div> </div> </div>

You are almost there. Use currentTarget to catch current element.

Note: Please avoid inline CSS, instead of inline use class.

 $(document).ready((elm) => { $('.label').on("click", (elm) => { var $this = $(elm.currentTarget); $('.label').removeClass('yourClss'); // Remove previous added class before add class in current markup $this.addClass('yourClss') }) })
 .yourClss { color: #ffffff }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="q1"> <img src="lizerds/l2.jpeg" class="img" /> <div> <div class="radioWrapper"> <label class="label"> <input type="radio" id="q1Snake" class="radio" name="q1radio"> Snake </label> </div> <div class="radioWrapper"> <label class="label"> <input type="radio" id="q1Lizard" class="radio" name="q1radio"> Legless Lizard </label> </div> </div> </div>

This problem is happening because you're trying to achieve this context with an arrow function.
As the official MDN documentation says:

  • Arrow function: "Does not have its own bindings to this or super, and should not be used as methods."

You should be doing this:

$(document).ready(() => {
  $('.label').on("click", function() {
    $(this).css("color", "#ffffff")
  })
})

Instead of this

$(document).ready(() => {
  $('.label').on("click", () => {
    $(this).css("color", "#ffffff")
  })
})

You have to catch current element in your function. Please use this code

$(document).ready((elem) => {
$('.label').on("click", (elem) => {
var current = $(elem.currentTarget);
$(current).css("color","#ffffff");
})
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM