How can I make an HTML textbox in C# MVC flash a background color for one second using TextBoxFor?

I'm currently working with MVC and have the following code in my razor view.

@Html.TextBoxFor(x => model.DateTimeStamp, new { @readonly = "readonly", @class=ViewBag.IsLatest ? "latest":""})

I want to briefly change the background color of this textbox to yellow for a second using some sort of transition effect. I don't have much experience with CSS, so any assistance would be appreciated.

Thank you.

Answer №1

If you want to implement this on the client side, here is a sample using CSS3 and jQuery.

jQuery(function () {
  jQuery("#blinkBtn").click(function () {
    jQuery("#DateTimeStamp").addClass("MakeYellow");
    setTimeout(function () {
      jQuery("#DateTimeStamp").removeClass("MakeYellow");
    }, 500);
  });
});
#DateTimeStamp {
  background-color: grey;
  transition: background-color 0.5s;
}

#DateTimeStamp.MakeYellow {
  background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="DateTimeStamp" />

<button id="blinkBtn">Blink</button>

If you want it to be conditional, you can try something like this in razor. It may not be the most elegant solution, but providing more context would help improve the implementation.

@if(ViewBag.MyCondition) {
    <script type="text/javascript">
    jQuery(function () {
        jQuery("#DateTimeStamp").focus();
        jQuery("#DateTimeStamp").addClass("MakeYellow");
        setTimeout(function () {
          jQuery("#DateTimeStamp").removeClass("MakeYellow");
        }, 500);
    });
    </script>
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble with targeting a specific css class

Can someone help me with a CSS issue I'm experiencing? Here is my code: <div class='test'> <div>test 1</div> <div>test 2</div> </div> <div class='test'> <div>test 3&l ...

Guide to Embedding Dynamic Images in HTML Using VUE

I am venturing into the world of VUE for the first time, and I find myself in need of a header component that can display an image based on a string variable. Despite my best efforts to search for tutorials, I have not been able to find a solution. Header ...

Combining Multiple Partial Views with Their Respective View Models in ASP.NET MVC: A Step-by-Step Guide

I am working on a view named Register where I include other views using @Html.Partial("ViewName"). I am curious about how to call other ViewModels from the partial views within this main view. While I know that each ViewModel with its fields can be declar ...

Simple Selection Panel

Looking to include a simple dropdown menu with 5 choices. Once a selection is made, an answer corresponding to that choice will appear in another field next to it. For example: Choosing France from the dropdown would display 'Paris' in the adjac ...

Vue's smooth scrolling in Nuxt.js was not defined due to an error with the Window

There seems to be an issue with adding vue smooth scroll to my nuxt.js project as I'm encountering the "window is not defined error". The steps I followed were: yarn add vue2-smooth-scroll Within the vue file, I included: import Vue from 'vue ...

Why does pressing "enter" create a line break in HTML when typing?

Apologies in advance for what may seem like a simple CSS issue that I couldn't find an answer to on Stackoverflow. Here's the JSfiddle link: https://jsfiddle.net/kwende/fqxna79a/ In the code, you'll see two div tags: <div id="left">L ...

Prevent triggering postback while validating during form submission in MVC

Is there a way to prevent postback for validation upon clicking Submit? I've noticed that after clicking submit, the page reloads/postbacks before displaying any validation errors. @model X.Models.ModelVm @using (Html.BeginForm()) { @Html.AntiFo ...

Tips for displaying and concealing columns in Blazor QuickGrid based on breakpoints

I encountered an issue when attempting to hide a PropertyColumn on small screens in Blazor 8 using the regular Bootstrap 5 method. By adding Class="d-sm-none d-md-block" to the QuickGrid component's PropertyColumn, it seems that the Bootstra ...

Arranging the list items in a perfectly straight vertical line

Concern: I'm struggling with alignment issues on my website. I have country flags displayed using display:inline;. The issue arises when the number next to the flag exceeds single digits (e.g., 10 or more) as it affects the alignment due to the consi ...

Ways to remove unwanted line breaks following PHP code within HTML documents

Currently working on building my blog, and I'm facing some challenges with the post div. It seems like every element within is automatically getting line breaks. Despite trying to float them or adjust padding, it's not giving me the desired layou ...

Unable to eliminate italicized text within collapsible content

Despite having no programming experience, I am attempting to create a FAQ section for our help site using collapsible sections for the Q&A. While I've managed to achieve most of what I wanted, there is one element that stubbornly refuses to change. De ...

Format six images in a horizontal row for desktop and ensure they resize appropriately for mobile viewing

I am looking to arrange images of different sizes in rows depending on the device orientation. For desktop, I want 6 images with proper margins, 2 images in a line for portrait, and 4 images for landscape orientation. Any extra images should be placed in a ...

Encountering a JavaScript runtime error while trying to access and interpret JSON

Currently, I'm facing a challenge with converting a C# list of string types into a JSON object. The issue arises when trying to read this JSON object later in JavaScript. On the other hand, the process seems to work fine when dealing with a C# list of ...

Unable to set DIV width to 100% and show a scrollbar

I am facing an issue with the width of a DIV element on my webpage. Despite setting it to 100% width, it only takes up the visible window's width and not the full page width, especially when a horizontal scroll bar is displayed. Below is the HTML cod ...

The combination of loading and scrolling JavaScript code is not functioning properly on the website

I created an HTML webpage that includes some JavaScript code to enhance the user experience. However, I encountered an issue when trying to incorporate a load JavaScript function alongside the scroll JavaScript function on my page. The load script is posi ...

Unable to navigate to input field in Ionic app while focusing on input field on mobile device

Default phone feature: When an input is focused on a mobile device, it automatically scrolls to that input. Issue: This functionality is not working in my ionic app due to horizontal scrolling. Consequently, when I tap on an input, it does not scroll to i ...

Setting session expiration for an HTML page in MVC with JavaScript - A step-by-step guide

I'm working on a HTML page within an MVC framework that includes a button click function. I'm trying to figure out how to redirect the user to the login page if the page remains idle for 30 minutes. I've attempted using the code snippet belo ...

Tips for altering the color of spans that share a common top position without affecting the surrounding sibling elements above or below

Is there a way to change the color of spans with the same top position, excluding sibling elements above or below, in a paragraph made up of spans? I've been working on how to change the color of the line of span tags that contain another span with t ...

What is the best way to adjust the size of a column when hovering

What I am attempting to achieve is, I would like the column box to have a 100% width, and when it is hovered over, I want it to transition to the left, with the width of the column box being about 60%, revealing a second block that shows up as 20% width o ...

The website functions properly with Live Server, but encounters issues when launched from the Files directory

After testing multiple browsers, the site consistently works fine with Live Server. However, when accessed through Firefox or Chrome, my style.css fails to load completely. On Edge, an error message indicates that the file is missing. Below are snippets of ...