How to simulate click only one button and not both?

Asked

Viewed 89 times

0

I think the question sounds confusing, but calm down, I’ll explain it better. Type, on a given page, there are 2 Buttons with the same class, having as difference only their value, and an attribute "origin". Example:

<input type='button' class='botao' onclick='envia(this,0);' origin='Roll < 49' value='Roll < 49'/>

<input type='button' class='botao' onclick='envia(this,1);' origin='Roll > 51' value='Roll > 51'/>

I tried to simulate the clicks using jquery, but I was unsuccessful as I clicked both buttons at the same time and not just one. My code:

var button1 = $('.clDicePlay').attr('value','Roll > 51'),
            button2 = $('.clDicePlay').attr('value','Roll < 49');

button1.trigger('click');

Someone can help me. Thank you very much in advance!

2 answers

1

Set a specific ID for each.

<input type='button' id='foo' class='bo..

With this, just apply the click Trigger

$('#foo').trigger('click');
  • Thank you for the reply Daniel. That wasn’t what I was looking for, but it helped me with something else. Thank you so much.

0


To take an element by the value in jQuery, use the selector value=[]. Example:

<input type='button' class='botao' origin='Roll < 49' value='Roll < 49'/>

<input type='button' class='botao' origin='Roll > 51' value='Roll > 51'/>
var button1 = $(':input[value="Roll < 49"]'),
    button2 = $(':input[value="Roll > 51"]');

button1.click(function(){
    envia(this, 0);
});

button2.click(function(){
    envia(this, 1);
});

button1.trigger('click');
  • That’s what I was looking for. I’m a beginner in Jquery. But thank you so much. It helped me a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.