-1

Currently i am doing as below

<script type="text/javascript">
$(document).ready(function () {
debugger;
$('#Draft').live('click', function () {
alert("I am Draft Here");
});

$('#Live').live('click', function () {
alert("I am live Here");
});

$('#Completed').live('click', function () {
alert("I am completed Here");
});
});
</script>

I am fetching each element's id and based on that invoking alert on click event of that.

Is there any way in which i can fetch all span elements, say whose style attribute is like style="color:

Below are like my span elements on page

<span style="color:#F3660E;" id="Draft">Draft: (2138)</span>

I am new to jquery, please suggest on this

Oleg K.
  • 1,549
  • 8
  • 11
Torakami
  • 177
  • 2
  • 12

3 Answers3

0

You can give them all a class, e.g.:

<span class="thingy" id="Draft">Draft: (2138)</span>

(You can remove the id unless you're going to use it for something.)

Then:

$(document.body).on("click", ".thingy", function() {
    // `this` will be the span element
});

Example:

$(document.body).on("click", ".thingy", function() {
  $("<p>").html("You clicked " + this.id).appendTo(document.body);
});
.thingy {
  color: #F3660E;
  border: 1px solid black;
}
<div>
  <span class="thingy" id="Draft">Draft: (2138)</span>
  <span class="thingy" id="Live">Live: (1234)</span>
  <span class="thingy" id="Completed">Completed: (4321)</span>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Note that in the above I used on, not live. live was deprecated several versions ago and removed a couple of versions back. on is how you do this with any recent version of jQuery.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

You can add a common class to all the elements.

<span class="myClass" id="Draft">Draft: (2138)</span>

Then you can bind event use class .myClass

$('.myClass').on('click', function () {
    //Here this refers to span, element which invoked the event
    alert("I am " + this.id + " Here"); 
});

DEMO

Satpal
  • 132,252
  • 13
  • 159
  • 168
0

You can also use click. For a short description of difference between click and on look at answer in Why use jQuery on() instead of click()

For your example I would probably just use //because it's shorter :)

$('.someClass').click(function(){
alert("I am someClass");
});
Community
  • 1
  • 1
Lau Frie
  • 59
  • 6