1

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.

3
  • 1
    It works? I don't know what the question is. The click event gets fired and an alert pops up just fine. Commented May 29, 2014 at 17:42
  • 3
    You're trying to trigger the .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 Commented May 29, 2014 at 17:45
  • Thank you at all. Your hints works fine for me. Thank you. Commented May 29, 2014 at 17:49

2 Answers 2

2

http://jsfiddle.net/azg2R/2/

Put the click event on top in the ready event.. The click event needs to be triggered after registering the event. It was not happening before

$(document).ready(function() {           
    // The function to be executed
    $('.list-group-item').click(function() {
        alert($(this).text());
    });

    // 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();
    }
});
Sign up to request clarification or add additional context in comments.

Comments

2

The problem is that you are triggering click event before attaching event handler to it. So you just need to move click handler before triggering click and everything would work as you expected:

$(document).ready(function() {
    // The function to be executed
    $('.list-group-item').click(function() {
        alert($(this).text());
    });
    // 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();
    }

});

JSFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.