Avatar of mainrotor
mainrotor
 asked on

I need help setting focus to an @Html.TextBoxFor in my MVC.Net application

Hi Experts,
I need help setting focus on a pageload to an @Html.TextBoxFor in my MVC.Net application.
How can I do this?  Here is my @Html.TextBoxFor code below (in a view).

@Html.TextBoxFor code
Thank you,
mrotor
.NET MVC.NET Programming

Avatar of undefined
Last Comment
mainrotor

8/22/2022 - Mon
Lokesh B R

Hi,

You can set the focus using the jQuery as follows.

<script type="text/javascript">

    $(document).ready(function () {

        $("#Qty").focus();
    });

</script>

Open in new window

mainrotor

ASKER
Lokesh,
Should I put the Script at the top of my view or towards the bottom?
Lokesh B R

Hi,

you can add below.

make sure you referenced the jquery before executing this script.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
mainrotor

ASKER
How do you reference jQuery?
I only have a refence to bootstap on the top of my view:

<link href="~/Content/bootstrap.css" rel="stylesheet" />

Open in new window


I am using Visual Studio 2015.  Don't know if that makes a difference on how to reference jQuery.
Lokesh B R

Hi,

jQuery will be referenced in _layout.cshtml
mainrotor

ASKER
I see it at the bottom of my _layout.cshtml page.
Does that mean that I don't have to include\reference it in my view?

jQuery.JPG
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Lokesh B R

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mainrotor

ASKER
Okay.  I will try it.
mainrotor

ASKER
That worked!!!!

Thank you.