Tuesday 15 February 2011

class - Jquery select clicked element within a set of elements with the same classname -


Maybe you guys can help me out of this, I've been struggling with it for the last 30 minutes.

Let me assume that I have four elements of the same class.

  & lt; Div class = "test" & gt; Hi & lt; / Div & gt; & Lt; Div class = "test" & gt; This is & lt; / Div & gt; & Lt; Div class = "test" & gt; Yo & lt; / Div & gt; & Lt; Div class = "test" & gt; What's up & lt; / Div & gt;   

How can I select the one I click on?

I was able to work it this way:

  $ ('.test'). Click (function () {$ (this) .toggleClass ("BTN-Danger BTN-Success");}); However, after the AJAX call I need to set the fire without clicking on success, so I should be able to do something like this (unsuccessfully attempted):  
  $ ('test', this). Toggle Class ("BTN-Danger BTN-Success"); // This $ (this) could not work ('. testing'). ToggleClass ("BTN-Danger BTN-Success"); // also did not work   

any suggestions? Thanks awfully!

It looks like one click is made by elements when AJAX calls, but in Ajax call Different values ​​need to be passed, on which basis that element is clicked on. Instead of using the "onclic" attribute, you can do the following:

HTML:

  & lt; Div class = "test" data-param1 = "value1a" data -param2 = "value2a" & gt; Hi & lt; / Div & gt; & Lt; Div class = "test" data-param 1 = "value 1b" data-param 2 = "value 2b" & gt; This is & lt; / Div & gt; & Lt; Div class = "test" data-param 1 = "value 1c" data-param 2 = "value 2c" & gt; Yo & lt; / Div & gt; & Lt; Div class = "test" data-param1 = "value1d" data-absolute 2 = "value 2d" & gt; What's up & lt; / Div & gt;   

Javascript:

  $ ('test'). Click (function () {var $ div = $ (this); $ .ajax (Url, {data: {param1: $ div.attr ('data-param1'), absolute 2: $ div.attr ('data -param2 ')}, success: function () {$ div.toggleClass ("BTN-Danger BTN-Success");}}};});   

Of course, Using the data - values ​​of the attributes.

No comments:

Post a Comment