Switch between text box and label appearances based on focus

I am working with a template field in my gridview. This template field consists of a text box. I am looking to achieve the functionality where after the user finishes writing in the textbox and moves to the next one, the textbox that loses focus will transform into a label or something similar. If it regains focus, it should turn back into a text box.

Any suggestions on how to implement something like this?

Answer №1

If you're looking for a way to edit content inline, there are several options available. One such option is the jQuery inline editing feature. You can check out this tutorial on how to implement it:

For a live demonstration of the jQuery inline editing in action, visit:

Keep in mind that when implementing inline editing, consider updating your database in a more efficient manner.

Answer №2

Here is an example of how to achieve this effect:

<script type="text/javascript">
function displayInput(){
document.getElementById("input-label").style.display = "none";
document.getElementById("input").style.display = "block";
document.getElementById("input").focus();
}

function hideInput(){
if(document.getElementById("input").value != "") document.getElementById("input-label").innerHTML = document.getElementById("input").value;
document.getElementById("input-label").style.display = "block";
document.getElementById("input").style.display = "none";
}
</script>

<label id="input-label" onclick="displayInput()" style="display:none">Enter text here</label>
<input type="text" id="input" onblur="hideInput()" />

Click here for a live demo

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

Having trouble retrieving data from the json file

Using Ajax to obtain a JSON update: $(document).ready(function(){ $('form').submit(function(event){ event.preventDefault(); var form = JSON.stringify($('form').serializeArray()); $.ajax ({ u ...

What is the best way to retrieve an HTML value using Impromptu?

I have integrated the impromptu jQuery plugin into my website to display modal prompts for users. I have included an input field of type "text" in the HTML section, but I am unsure of how to retrieve this value in order to send it via an AJAX request. va ...

Tips for positioning text within an input field above a div element

When a div is placed behind another div, the text in both div elements aligns. .wrapper { box-sizing: border-box; padding: 0; margin: 0; outline: none; padding-right: 20px; } .box { height: 34px; width: 130px; padding: 6px 12px; fon ...

Troubleshooting the problem with file uploads in the jQuery Form Plugin caused by a string being returned

Trying to utilize the jQuery Form Plugin for file uploads, specifically targeting an ASP.NET MVC controller action with a return type of string. Controller Action Returning String [HttpPost] public string PracticeInfoFormUpload(HttpPostedFileBase myfil ...

Can a dundas chart control be enhanced by utilizing a partial class?

Is there a way to extend a proprietary control like the Dundas chart control without access to the source code by using partial classes? I initially believed it could be accomplished simply by referencing the DLL, but I have encountered difficulties in tr ...

Is it possible to determine the status of the cover by inspecting the element?

Recently, I've been contemplating the idea of adjusting my background image to move around a bit. It seems like a fancy concept and not too difficult to execute. However, I am facing an issue because my background image is currently set to cover (back ...

Mastering the Menubar in ASP.NET MasterPages

I have a main page and four other pages that are linked to it. <div class="navcover"> <div class="nav" style="background-image: url(images/lion.jpg);"> <a href="Default.aspx">Home</a> </div> <div class= ...

"An error occurred with the Google chart due to 'null' not being recognized as an object, specifically in relation to the select menu and onchange event

Currently, I have implemented the following Script: <header> <script type="text/javascript" src="http://www.google.com/jsapi"></script> </header> <body> <script type="text/javascript"> google.load("visualization", "1 ...

I've been attempting to fill a multi-dimensional array using a datatable, but I've had no luck so far

I'm currently facing an issue where I am trying to populate a multidimensional array with data, but consistently encountering the following error: "Cannot implicitly convert type 'Object[][]' to 'string[,]'. I am unsure how to reso ...

Viewing a webpage in the format of an Excel document, specifically using .xlsx file extensions

Looking for a better solution here. A while back, I discovered that I could convert a web page with a Gridview or table into an Excel file easily. It maintained the formatting of colors and alignment from the HTML page. However, in newer versions of Excel, ...

What is the best way to maximize an image's height evenly outside of its full-width container?

Is it possible to extend an image beyond its parent container on both the top and bottom while maintaining responsiveness and containing remaining content within the parent? I've managed to achieve this effect on the top but struggle with the bottom. ...

What is the best way to position my Jchartfx area graph below my gridview?

When my page loads, the graph appears like this. It consistently shows up in the top left corner even though it should be positioned underneath the grid view as intended. var chart1; function createGraph(mpy) { if (mpy == undefined) mpy = 12.00; ch ...

Tips on adjusting the width of a div container based on the size of an

I am struggling with setting the width of my "picBox" div equal to the image tag inside. I have tried adjusting the CSS code but cannot seem to get it right. Any suggestions on how to solve this issue would be greatly appreciated. Thank you. The CSS code: ...

The impact of a water droplet image on the reaction

Hello everyone, I am seeking assistance regarding CSS as the chatroom dedicated to CSS is currently empty. I am attempting to create a water drop effect but haven't had much success so far. Could someone guide me in the right direction? I have tried u ...

The jQuery $.ajax request is still in a pending state

I've developed a straightforward chat application that utilizes the long-polling method with jQuery. function sendchat(){ // This function sends the message $.ajax({ url: "send.php", async: true, data: { /* insert inputbox1.value */ }, succe ...

Aligning text vertically in a div using Bootstrap, alongside additional content within the same div

I'm attempting to vertically align some text within a div that also contains an image. When I apply the classes "d-flex align-items-center," the text does move to the center, but it appears slightly shifted towards the top. How can I fix this issue? ...

:hover does not activate the animation forwards

Why won't the animation: forwards work on :hover? The animation itself is functioning properly, but the command "forwards" isn't. The Issue: Here's the HTML code snippet: <img src="images/notebook_desk.svg" class="notebook-cover"> ...

JavaScript consistently returns HiddenField as NULL

After creating a Context menu and associating it with a Gridview, I noticed a problem when pressing a button from the context menu. I intended to retrieve a value from a HiddenField, but it always returns null. My assumption is that the DOM might not be fu ...

Optimize several projects or pages with RequireJS

How can I improve the efficiency of my multiple requirejs projects? For example, I have this structure below with two main.js in different locations/folders, build/ build.js dev/ index.php core/ js/ main.js libs/ ...

Using the jQuery .each() method to generate an array of objects

I have a goal of creating something similar to this. var data = google.visualization.arrayToDataTable([ ['Year', 'Cost'], ['2004', 1000], ['2005', 1170], ['2006', 660], ['2007&a ...