0

I'm trying to make a link click whenever the page reloads with jQuery, however this code is not working:

@model IEnumerable<SPMVCApp.Models.Picture>
@{
    ViewBag.Title = "Index";
 }
<h2>@ViewBag.Mb Mb</h2>
<p>
    @Html.ActionLink("Create New", "Create", null, new { id = "btn" })
</p>
<table>
    <tr>
        <th></th>
    </tr>
</table>

<script type ="text/javascript">
    $(document).ready(function(){
      jQuery('#btn').click()
    })
</script>

I tried using $ instead of jQuery when firing the click but still no luck, I haven't got much experience with jQuery/JavaScript

Vikas Arora
  • 1,666
  • 2
  • 17
  • 38
ab1428x
  • 794
  • 2
  • 8
  • 20
  • 1
    Have you tried `jQuery('#btn').trigger('click')`? – Satpal Jan 10 '14 at 07:52
  • 1
    Try @id = "btn" check your source with firebug or some other inspector, try debuging in javascript. I prefer also to use $("#btn").trigger("click") but that should not matter. Also your document ready does not have ;. – Vladimir Bozic Jan 10 '14 at 07:52
  • One important thing to do not use Html.ActionLink if you have a lot of links it will slow down your page load, but that is another subject :) – Vladimir Bozic Jan 10 '14 at 07:55
  • Satpal thanks, that didn't help however. @VladimirBozic, what should be used instead of ActionLink would you say? – ab1428x Jan 10 '14 at 08:05
  • @VladimirBozic I tried @id="btn" and using trigger and still no results. I know syntax should be proper because I tried $("btn").css("font-size", 5) and it changes the font upon load so I'm quite confused at this point. – ab1428x Jan 10 '14 at 08:21
  • I use Url.RouteUrl and Html.RouteLink where ever possible. ActionLink can slow down page load a lot if you have a lot of link's but not if you don't have a lot of custom routes, if you do than avoid ActionLink cause searching tru route dictionary takes time. – Vladimir Bozic Jan 10 '14 at 09:49
  • Further on the main problem, is there correct link on your element. You can also try calling it like this : document.location.href = $("#btn").attr("href"); – Vladimir Bozic Jan 10 '14 at 10:04
  • @VladimirBozic how should I connect this with the click? – ab1428x Jan 10 '14 at 12:58
  • You can call it from click handler $("#btn").click(function(){document.location.href = $("#btn").attr("href");}); – Vladimir Bozic Jan 10 '14 at 14:22
  • What browser do you use for debugging? – Vladimir Bozic Jan 10 '14 at 14:23
  • Possible duplicate of [How can I simulate an anchor click via jquery?](https://stackoverflow.com/questions/773639/how-can-i-simulate-an-anchor-click-via-jquery) – Michael Freidgeim May 25 '18 at 13:22

4 Answers4

1

You can use this code:

$(function(){
    $("#btn").click(function(){
        document.location.href = "YourUrlFor Refresh"
    })
})

also do not remember that include your page to Jquery Script like 1.8.1

Saeed
  • 3,415
  • 3
  • 24
  • 41
  • No need for -1 for @Leo missing semi-collons it is not syntax error, but plain simple bad code writing habit, and i did find that i can cause errors in some older browsers... – Vladimir Bozic Jan 10 '14 at 07:59
0

By seeing your code I understood that you want to call Create Action of your controller. So instead of calling click event of your button you call call that action directly from your javascript using ajax.

Se bellow e.g.

$.get("Home/Create", function (result) {
  // do wahtever you want to do with result
});
vaibhav shah
  • 4,939
  • 19
  • 58
  • 96
0

you can debug it in console at chrome or firefox .

check whether jQuery('#btn') is [] or the elemnt you want.

Loda
  • 31
  • 4
0

Ok I fixed it.

I just need to add [0] after the parameter as so:

$('#btn')[0].click()

or

jQuery('#btn')[0].click()

ab1428x
  • 794
  • 2
  • 8
  • 20