I tried to trigger a click event on an selected DOM element but my code won't work.
You can see my attempt on JSFiddle.
<ul class="list-group">
<a href="#" class="list-group-item" data-key="32">LG GOLA 8M</a>
<a href="#" class="list-group-item" data-key="33">LG 5-6M</a>
<a href="#" class="list-group-item" data-key="34">LP 5-6M</a>
</ul>
$(document).ready(function() {
// I get the string tr from URL parameters
var tr = "fix_LG%20GOLA%208M";
if (tr !== null) {
var terminrahmen = $('.list-group-item').filter(function() {
return $(this).text() === decodeURI(tr).substring(4);
});
// Trigger click event on .list-group-item
terminrahmen.click();
}
// The function to be executed
$('.list-group-item').click(function() {
alert($(this).text());
});
});
When the DOM was loaded I collect some data from URL parameters and compare the data with DOM elements. This part works fine.
After that I get an element and I would like to trigger an click event. The click event should "execute" a specified function.
Have anyone a good solution for me? Thanks in advance.
.click()before you've assigned the handler. Naturally that isn't going to work. Assign the handler first so that it's there when you trigger it. jsfiddle.net/azg2R/4