simulating click event of a different selector

Undefined Variable

I have a bunch of product buttons like:

    <button class='prd-class' data-prd-id='1'>
      Product 1
    </button>

    <button class='prd-class' data-prd-id='2'>
      Product 2
    </button>

And I have a button click function like so:

$('.prd-class').click(function(){
    $('.prd-class').removeClass('cls-focus'); //remove any focused product
    $(this).addClass('cls-focus'); //then focus on the selected one
    $('#selected-prd-name').text($(this).data('prd-name'));
    ... etc
});

As you can see, it uses this object reference inside the function heavily.

Now there is another situation where at page load I want the lines inside this function to be executed.

Since there are multiple product buttons, I want to ensure that the I am simulating the click event of the required one.

$(document).ready(function()
{
   $("button[data-prd-id='"+prd_id+"']").click();
});

But this does not work. How can I change the code to execute the code lines correctly?

w3dev

I am not sure about your requirements. However, this demo might give you some ideas to resolve your issues.

HTML:-

<button class='prd-class' data-prd-id='1'>Product 1</button>
<button class='prd-class' data-prd-id='2'>Product 2</button>
<div>
    Selected Product ID: <span id="selected-prd-name"></span>
</div>

CSS:-

.cls-focus {
    border: 1px solid red;
    background-color: brown;
}

JavaScript:-

(function () {
    var $prdClass = $('.prd-class'),
        $selectedPrdId = $('#selected-prd-name'),
        prdClassClickHander = function () {
            var $self = $(this);
            $prdClass.removeClass('cls-focus');
            $self.addClass('cls-focus');
            $selectedPrdId.text($self.data('prd-id'));
        },
        init = function () {
            $prdClass.on("click", prdClassClickHander);
        };
    $(document).ready(init);
}());

// Simulate the click on DOMReady
$(document).ready(function () {
    var prd_id = 1;
    $("button[data-prd-id='" + prd_id + "']").trigger('click');
});

JSFiddle Demo:- http://jsfiddle.net/w3devjs/e27jQ/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

simulating click event of a different selector

From Dev

click event on different child selector with same selector

From Dev

State is not getting changed after simulating click event

From Dev

JQuery selector on click event

From Dev

Bind click event to selector with knockout

From Dev

jQuery click event on :data selector

From Dev

Bind click event to selector with knockout

From Dev

multiple executions when bind click event on * selector

From Dev

selector not working after append inside the .on() click event

From Dev

Jquery click event on selector returns child as target

From Dev

How to toggle hover event for another selector on click?

From Dev

AmCharts, capturing click event for period selector

From Dev

selector not working after append inside the .on() click event

From Dev

Not able to access click event with jQuery selector

From Dev

Removing ".on" Click Event in jQuery using 'selector;

From Dev

Add event handler on click with specific selector

From Dev

Simulating a mouse click in R

From Dev

Simulating OnClick event for DataGridViewCheckBox?

From Dev

Simulating event system using pandas

From Dev

Simulating Touch Event in Windows 8

From Dev

How to trigger click event if selector is loaded from external

From Dev

CasperJS: reference to a DOM element in click event instead of selector

From Dev

Dynamic selector for on click event acting strangely after 2 events?

From Dev

How to get selector on which 'click' event has been bound?

From Dev

How to trigger click event if selector is loaded from external

From Dev

Meteor using different Sessions with {{#each}} for click event

From Dev

Error simulating click on menu item with Robolectric

From Dev

Simulating a mousedown, click, mouseup sequence in Tampermonkey?

From Dev

Simulating click and change with Prototype - Simulate.js

Related Related

  1. 1

    simulating click event of a different selector

  2. 2

    click event on different child selector with same selector

  3. 3

    State is not getting changed after simulating click event

  4. 4

    JQuery selector on click event

  5. 5

    Bind click event to selector with knockout

  6. 6

    jQuery click event on :data selector

  7. 7

    Bind click event to selector with knockout

  8. 8

    multiple executions when bind click event on * selector

  9. 9

    selector not working after append inside the .on() click event

  10. 10

    Jquery click event on selector returns child as target

  11. 11

    How to toggle hover event for another selector on click?

  12. 12

    AmCharts, capturing click event for period selector

  13. 13

    selector not working after append inside the .on() click event

  14. 14

    Not able to access click event with jQuery selector

  15. 15

    Removing ".on" Click Event in jQuery using 'selector;

  16. 16

    Add event handler on click with specific selector

  17. 17

    Simulating a mouse click in R

  18. 18

    Simulating OnClick event for DataGridViewCheckBox?

  19. 19

    Simulating event system using pandas

  20. 20

    Simulating Touch Event in Windows 8

  21. 21

    How to trigger click event if selector is loaded from external

  22. 22

    CasperJS: reference to a DOM element in click event instead of selector

  23. 23

    Dynamic selector for on click event acting strangely after 2 events?

  24. 24

    How to get selector on which 'click' event has been bound?

  25. 25

    How to trigger click event if selector is loaded from external

  26. 26

    Meteor using different Sessions with {{#each}} for click event

  27. 27

    Error simulating click on menu item with Robolectric

  28. 28

    Simulating a mousedown, click, mouseup sequence in Tampermonkey?

  29. 29

    Simulating click and change with Prototype - Simulate.js

HotTag

Archive