Dylan Czenski Dylan Czenski - 1 month ago 13
jQuery Question

jQuery cannot trigger a click on a link

I have a link in the view:

<small><a href="javascript:void(0);" id="morelink">Show more</a></small>


And it's event is handled in jQuery:

$("#morelink").click(function () {
$('#displayDiv').show;
return false;
});


And I want to trigger a click on the link after I hit any button in the
editBtn
class:

$('.editBtn').on('click', function () {
$('#morelink').trigger('click'); // not triggered
});


How ever the link is not triggered in IE or Chrome. Can anyone tell me why?

Answer Source

Syntax Error. See docs of .show() here

Change

$('#displayDiv').show;

to

$('#displayDiv').show();

$("#morelink").click(function() {
  $('#displayDiv').show();
  return false;
});
$('.editBtn').on('click', function() {
  $('#morelink').trigger('click'); // not triggered
});
#displayDiv {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<small><a href="javascript:void(0);" id="morelink">Show more</a></small>

<button class="editBtn">Edit A</button>
<button class="editBtn">Edit B</button>
<button class="editBtn">Edit C</button>
<div id="displayDiv">DISPLAY DIV</div>