0

I have the below button on my page. I want below button to be auto clicked when this page gets loaded. I've tried following various suggestions on the net but I'm unable to find a solution. Any help is appreciated.

 <a data-bind="click: $root.addtocart" class="w-button btn-large" 
    style="text-decoration: none!important;" id="redeemButton">
                        Redeem Now                                                                               
 </a>
GôTô
  • 7,974
  • 3
  • 32
  • 43
pradeep
  • 1
  • 2
  • 1
    you can call the `addtocart()` method on page load – Uday Feb 21 '17 at 09:02
  • hi you can do something like this.... http://stackoverflow.com/questions/18646881/auto-click-button-element-on-page-load-using-jquery – Isha Goyal Feb 21 '17 at 09:03
  • 1
    Possible duplicate of [Auto-click button element on page load using jQuery](http://stackoverflow.com/questions/18646881/auto-click-button-element-on-page-load-using-jquery) – Vishal Patel Feb 21 '17 at 10:02
  • @VishalPatel there is no mention here of the presence of jQuery – GôTô Feb 21 '17 at 13:11

2 Answers2

3

Do it directly in the creation of your viewmodel:

function MyViewModel(){
    this.addtocart = function(){
      //Some code
    }
    this.addtocart(); // <-- here
}
GôTô
  • 7,974
  • 3
  • 32
  • 43
  • If i add " this.addtocart();" it is auto clicking but the data that is printing is different from the data printed when i manually click on button. – pradeep Feb 21 '17 at 11:16
  • @pradeep well we don't have the details of what your code does – GôTô Feb 21 '17 at 11:19
  • When you bind `click: $root.addToCart`, it sends the clicked item as a first argument. (i.e.: `addToCart($data, event)`) Like @GôTô said, you haven't shown us `addToCart`, but I can image it needs to be sent a parameter in order for it to work. – user3297291 Feb 21 '17 at 14:39
0

you can add a ready function in your javascript like this..

<script>
    $("document").ready(function () {
        window.getElementById("redeemButton").click();
    });
</script>
Isha Goyal
  • 95
  • 1
  • 4
  • 14
  • That's not really the Knockout way (although it should work). When using a binding framework, jQuery should be avoided as much as possible – GôTô Feb 21 '17 at 09:14
  • If i paste the above code in my hbs file control is going into function but click is not happening. – pradeep Feb 21 '17 at 11:17