0

I've been rummaging through the web to how to click buttons for a project I'm working on with Javascript+JQuery(Still not very good). Luckily I was able to find my answer and figured out how to click basic HTML buttons that looked like this(Cut off some of the code just to get the point across).

<div class="ui-block-a">
    <input type="submit" value="Buy Now" data-theme="d">
</div> 

But now I've come across a button like this.

<div id="BuyWithRobux">
    <div data-expected-currency="1" data-asset-type="T-Shirt" class="btn-primary btn-medium PurchaseButton " data-se="item-buyforrobux" data-item-name="Donations" data-item-id="170938328" data-expected-price="2" data-product-id="20832770" data-expected-seller-id="180359" data-bc-requirement="0" data-seller-name="Clone3102">
        Buy with R$
        <span class="btn-text">Buy with R$</span>
    </div>
</div>

Just based off of what I know I don't think I can use what I used to click the last button which was...

 $("input[type='submit']").click();

So my question is how can I click this "button"? I've tried using my old code on it to no avail. I rather not use Selenium, or anything if at all possible. Could anyone help me out with this? If you need anymore information just say what and I'll do my best to provide it, fairly new to this so don't know what to include sadly.

Thank you in advance.

3
  • You need to learn CSS selector syntax, which is what jQuery uses to address its elements. In this case, the selector you need is going to depend on what element has the click handler attached to it. If it's the span, for example, you could use #BuyWithRobux .btn-text. Commented Aug 11, 2014 at 1:50
  • The new format can simply be $('.btn-primary').click(), but if this is a Bootstrap example you should be able to bind to .btn so you can catch all the button states in one handler. You should also try and have something in common between your regular submit button and your Bootstrap(esque) buttons so you can capture all of them in one event handler. I know that Bootstrap recommends/requires you to add .btn etc to your regular submit buttons anyway, so that would be fine... Commented Aug 11, 2014 at 1:50
  • Alright thank you both, got some useful information from both of your answers. This really helps, again thank you this solves all of my issues. Commented Aug 11, 2014 at 2:23

1 Answer 1

1

By "clicking this button" If you meant to trigger a click on div#BuyWithRobux , You can do so like

$("#BuyWithRobux").click();

or

$("#BuyWithRobux").trigger("click");

The syntax for triggering a click remains the same, all you've to do is to use the right selector to target the element on which you need to trigger the event.

You can learn more about jQuery selectors here

Sign up to request clarification or add additional context in comments.

3 Comments

Might have to use $("#BuyWithRobux")[0] as I think it's storing the DOM element(s) in an array automatically. Could be wrong about ID selectors though..
@KevinDamstra You could've atleast tried it before commenting.
Sorry for stepping on your toes bro, just something I encountered once using selectors like that. Just trying to help ^^

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.