Mr. Nobody
Mr. Nobody

Reputation: 89

on click of multiple elements in jquery?

I have:

<div id="d1">
   <div id="d2"></div>
   <div id="d3"></div>
   <div id="d4"></div>
</div>

What I want is in which ever of the above element is clicked call this function:

function clickedele(){
    alert("Ele clicked");
}

I tried these But none worked:

var mhele_1 = $('#d1');
var mhele_2 = $('#d2');
var mhele_3 = $('#d3');
var menu_ico =$('#d4');
$([menu_ico.get(0),mhele_1.get(0), mhele_2.get(0),mhele_3.get(0)]).on('click', function(){
    alert("Ele clicked");
});

or

$('#menu_ico,#d1, #d2,#d3').on()

or

$('#menu_ico').add('#d1').add('#d2').add('#d3').on()

But none worked

Upvotes: 5

Views: 6757

Answers (7)

Abhijit
Abhijit

Reputation: 252

You can give child divs a common class. Lets suppose the class name is childDiv. Then you can handle the click event of the divs.

$(".childDiv").on("click",function(){
    alert("Ele clicked");
})

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115282

Your selector #menu_ico does not point to the element the element ids are d1, d2 and d3 so combine the id selectors and bind click event handler.

$('#d2,#d3,#d4').on('click', function(){
    alert("Ele clicked");
});

$('#d2,#d3,#d4').on('click', function() {
  alert("Ele clicked");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="d1">
  <div id="d2">a</div>
  <div id="d3">b</div>
  <div id="d4">c</div>
</div>


UPDATE 1: If the elements are dynamically added then use event delegation.

// assumes `#d1` is not dynamically added,
// if yes then use any of it's ancestor present
$('#d1').on('click', '#d2,#d3,#d4', function(){
    alert("Ele clicked");
});

$('#d1').on('click', '#d2,#d3,#d4', function(){
    alert("Ele clicked");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="d1">
  <div id="d2">a</div>
  <div id="d3">b</div>
  <div id="d4">c</div>
</div>


FYI : Use your code within document ready handler to run after elements are loaded or add code at the end of the page.


UPDATE 2: The better approach would be using a common class for all elements and bind the handler to that.

$('.d').on('click', function() {
  alert("Ele clicked");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="d1">
  <div id="d2" class="d">a</div>
  <div id="d3" class="d">b</div>
  <div id="d4" class="d">c</div>
</div>

Upvotes: 10

th1rdey3
th1rdey3

Reputation: 4388

Event Delegation should be the way to go.

$('#d1').on('click', clickFunc)

function clickFunc(e) {
  alert($(e.target).text())
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="d1">
  <div id="d2">1</div>
  <div id="d3">2</div>
  <div id="d4">3</div>
</div>

Upvotes: 1

Emad Dehnavi
Emad Dehnavi

Reputation: 3451

HTML :

<div id="d1">
   <div id="d2" onclick="isClicked(this)">d2</div>
   <div id="d3" onclick="isClicked(this)">d3</div>
   <div id="d4" onclick="isClicked(this)">34</div>
</div>

javascript :

function isClicked(obj){
   alert(obj.id);
} 

https://jsfiddle.net/emilvr/dyzgcju5/

Upvotes: 0

guradio
guradio

Reputation: 15565

$('.common').click(clickedele)

function clickedele() {
  alert("click from " + $(this).text())
  alert("Ele clicked");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="d1">
  <div id="d2" class="common">1</div>
  <div id="d3" class="common">2</div>
  <div id="d4" class="common">3</div>
</div>

  1. Add class common to all.
  2. Use class to click

$('#d1 div').click(clickedele)

function clickedele() {
  alert("click from " + $(this).text())
  alert("Ele clicked");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="d1">
  <div id="d2" class="common">1</div>
  <div id="d3" class="common">2</div>
  <div id="d4" class="common">3</div>
</div>

  1. Use the parent as selector then add space and div meaning child of parent select like $('#d1 div')

Upvotes: 0

Atul Sharma
Atul Sharma

Reputation: 10740

function clickedele() {
  console.log('clicked');
}
$('div[id^="d"]').on('click',clickedele);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="d1">
   <div id="d2"></div>
   <div id="d3"></div>
   <div id="d4"></div>
</div>

use $('div[id^="d"]') to select all div with id starting with d you can use any other pattern also like $('#d1 > div[id^="d"]') to select all div with id starting with d inside d1 div.

Upvotes: 0

Sander Meijer
Sander Meijer

Reputation: 85

Its better to use a loop for that:

$('#d1 > div').each(function(){
 $(this).click(function(){
    alert("Ele clicked");
 });
});

Upvotes: -2

Related Questions