Greg Bala
Greg Bala

Reputation: 3903

jQuery delegate() / on() - force order of events

Perhaps a silly question but...

In this simple code:

 $(".Q").on("click", ".container", containerClick);
 $(".Q").on("click", ".container .oneElement", oneElementInContainerClicked);

if .oneElement in .container is clicked, first oneElementInContainerClicked is called.

Is is possible to force the order of events such that containerClick is called first, then oneElementInContainerClicked?

Upvotes: 0

Views: 118

Answers (1)

guest271314
guest271314

Reputation: 1

Try including check for e.target within containerClick ; calling .trigger("click") on $(this).parents(".container") within oneElementInContainerClicked

function containerClick(e) {
  if (e.target.className !== "oneElement")
    console.log("container")
} 

function oneElementInContainerClicked() {
  console.log("oneElement");
  $(this).parents(".container").trigger("click")
} 


$(".Q").on("click", ".container", containerClick);
 $(".Q").on("click", ".container .oneElement", oneElementInContainerClicked);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="Q">
  <div class="container">container
    <div class="oneElement">oneElement</div>
  </div>
  </div>

Upvotes: 1

Related Questions