简体   繁体   中英

change the background of an id that is clicked

When one of the element(id) of a form is clicked, i would like to listen to that event, and change the background of that element.

$(document).ready(function(){
    $('form').on('click', function(e){        
        var x = e.target.id;
        $(x).css('background-color',color);
        return false;
    });
}


<form>
  <div id="a">Item1</div>
  <div id="b">Item2</div>
  <div id="c">Item3</div>
</form>

Your code will end up looking for tag names because the selector is

$("b")

If you want to do it the way you have it, you would need to add the missing # .

$("#" + x).css('background-color',color);

But there is no need to look up the element when you already have a reference to is. Use event delegation and this

$('form').on('click', 'div', function(e){        
    $(this).css('background-color',color);
});

Why bother using e.target ? Just update your code to use $(this) :

$(function() {  
    $('form > div').on('click', function(e) {  
        e.preventDefault();
        $(this).css('backgroundColor', color);
    });
});

This will work out. Take care of your tags.

 $(document).ready(function(){
    $('form').on('click', function(e){   
        var x = e.target.id;
        $("#"+x).css('background-color',"red");
        return false;
    });
});

The thing happening to you is that event.target.id returns a string representing the id of the element, not a selector. So where you use $(x).... you have to use $('#'+x), your actual code does not work because the selector for the background change is not looking for an element with the X value on the Id but for an element called like the value of X (eg. x="a", then it's looking for elements)

Here's my "lowly" try:

<!DOCTYPE html>
<html lang = 'es'>
    <head>
        <title> My Test </title>
    </head>
    <body>
        <form>
          <div id="a"> Item1 </div>
          <div id="b"> Item2 </div>
          <div id="c"> Item3 </div>
        </form>
        <script>
            function addBackgroundColor(e){
                var index = parseInt(Math.random() * 4); // the number of the multiplier has to be equal to the length of the array colorsList.
                e.target.style.backgroundColor = colorsList[index];
            }

            var colorsList = ['blue','red','green','yellow','orange']; //add others colors here if you want.
            var divsList = document.getElementsByTagName('div');
            for (var i in divsList){
                divsList[i].addEventListener('click', addBackgroundColor);
            }   

        </script>
    </body>
</html>

No need to go fancy. Just do

$(document).ready(function(){
    $('form div').on('click', function(e){        
        $(this).css('background-color',color);
        return false;
    });
}

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