Hello! Tell me, please, how can I pass an id (div) when I click on it (below is an example)

<div class="Content" id="div3.1" onClick="Func_Id_Div" >Папка 3.1</div> 

And when performing this function, it is necessary that the "Folder 3.1" be filled with a light blue color (and in parallel all the other divs who have been cleared from the fill. Well, I think so, you must first clear all the divs from the fill and then click on the fill. But I can not register it.

  • onClick = "Func_Id_Div (this.id)" pass the id like that, but the fill is not clear without the code - makregistr
  • Thank you all for the decision nashol (pushed for a good idea) and I forgot to point out that you can not use the frameworks (and jquery treats them unfortunately :)) Once again, thank you very much !!!! - Vitaliy44

4 answers 4

  <style> .content { width: 100%; background-color: lightgrey; height: 100px; } </style> <div class="content" id="div3.1">Палка 3.1</div> <div class="content" id="div3.2">Палка 3.2</div> <div class="content" id="div3.3">Палка 3.3</div> <script> $(".content").click(function() { alert(this.id); $(".content").css("background-color", ""); $(this).css("background-color", "red"); }); </script> 

http://jsfiddle.net/54Awe/2/ I understand, ultimately, this is necessary?

    All you have to do is transfer the div itself (that is, yourself) to the function, on which the event occurred:

     <div class="Content" id="div3.1" onClick="Func_Id_Div(this)" >Папка 3.1</div> <script> function Func_Id_Div(obj) { // Some actions with div-object.... } </script> 
    • But this is already interesting! What is a minus? Justify. --- Or is it @ karmadro4 again got a minus and ran into its hole? - AseN

    gut, doesn’t it seem like jQuery is too much for such a task?

    function Func_Id_Div (obj) {return this.id; }

       $('.Content').click(function(){ var id = $(this).attr('id'); $('.Content').css('background-color','white'); $(this).css('background-color','blue'); }); 
      • 3
        As it infuriates me that almost everyone initializes $(this) 100,500 times $(this) , do you understand what this code does? And the main question is var id = $ (this) .attr ('id'); // wtf? what's the point in this line? var id = this.id; // everything, why is there to initialize a new jquery object? - Zowie
      • @AlexWindHope, as always support. - AseN