Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6827427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:15:52+00:00 2026-05-26T22:15:52+00:00

I have an ASP.net MVC2 site that I have designed. I have a small

  • 0

I have an ASP.net MVC2 site that I have designed. I have a small design issue. I want a button to change when the view loads. For instance when a certain variable equals a certain value I want one button to show up and another button to disappear. What I have so far is basically a jquery implementation of that idea. But so far, my jquery code is not getting called (I have a debugger statement set inside the jquery so I know it’s not getting called). I have tried to place the code in a $(document).ready(function()… and a $(document).load(function()…

the code still doesn’t load. here is what I tried.

$(document).load(function(){           
        var $b = <%=toggle %>;
        var buttonTog = $("#test1");
        var buttonTog1 = $("#Submit1");
        if($b=="1")
        {
            debugger;
            buttonTog1.css({
                display: 'block'
            })
            buttonTog.css({
                display: 'none'
            });
        }
        else if($b=="0")
        {
            debugger;
            buttonTog1.css({
                display: 'none'
            })
            buttonTog.css({
                display: 'block'
            });
        }   
});

$(document).ready(function () {
    //debugger;
    ShowMessage();
    (function () {           
        var $b = <%=toggle %>;
        var buttonTog = $("#test1");
        var buttonTog1 = $("#Submit1");
        if($b=="1")
        {
            debugger;
            buttonTog1.css({
                display: 'block'
            })
            buttonTog.css({
                display: 'none'
            });
        }
        else if($b=="0")
        {
            debugger;
            buttonTog1.css({
                display: 'none'
            })
            buttonTog.css({
                display: 'block'
            });
        }            
    });
    ...
    });

What I intend to do is check the value of a variable that I get from a ViewData field, and based upon what that value is make the css display attribute of one button or the other be either none or block depending upon circumstances of course. What is the best way to get Jquery code to run when a page first fires? And how would you implement such a scheme? Is this the best way to get this done, or should I consider redesigning how I implement this feature? Thank you for your time.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:15:52+00:00Added an answer on May 26, 2026 at 10:15 pm

    Let’s try to clean your code a little. Let’s suppose that toggle is a boolean variable defined previously in the page although this is bad and you should use view models:

    <% bool toggle = true; %>
    
    <script type="text/javascript">
        $(function() {
            var buttonTog = $("#test1");
            var buttonTog1 = $("#Submit1");
            var toggle = <%= new JavaScriptSerializer().Serialize(toggle) %>;
            if (toggle) {
                buttonTog1.show();
                buttonTog.hide();
            } else {
                buttonTog1.hide();
                buttonTog.show();
            }
        });
    </script>
    

    But assuming you have this information on the server (and you have it since you are defining the toggle variable on the server) I would recommend you directly applying the corresponding CSS classes to the given buttons on the server. This way you don’t even need to use any javascript at all. Simply:

    <button type="submit" id="test1" class="<%= Html.ToggleClass(toggle) %>">Test1</button>
    <button type="submit" id="Submit1" class="<%= Html.ToggleClass(!toggle) %>">Submit1</button>
    

    where ToggleClass would be a custom HTML helper returning the correct CSS class name based on the parameter value:

    public static class HtmlExtensions
    {
        public static string ToggleClass(this HtmlHelper htmlHelper, bool toggle)
        {
            return toggle ? "visible" : "hidden";
        }
    }
    

    and finally in your CSS file:

    .visible {
        display: block;
    }
    
    .hidden {
        display: none;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a site in ASP.NET 4 and MVC2 that will have premium
I have an ASP.NET MVC2 site connecting to a MySQL database via DbLinq. There
I have an asp.net mvc2 application that is using StructureMap 2.6 and NHibernate 3.x.
I have a ASP.NET MVC2 project in VS2010 that can be deployed in two
We have an ASP.Net MVC2 web site, and are utilizing EF4 for database access,
I have a dropdownlist in my asp.net MVC2 view like this: <% using(Html.BeginForm(temp,Settings)){ %>
I have 2 asp.net mvc2 projects in a solution. One is normal site for
We have a white-labelled platform built on Asp.Net MVC2. At present the site has
I have an existing asp.net mvc website that uses basic forms authentication. The site
I have an IIS7.5 web-site, on Windows Server 2008, with an ASP.NET MVC2 web-site

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.