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 79079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:05:22+00:00 2026-05-10T21:05:22+00:00

I’m trying to single source a form page which can be in edit mode

  • 0

I’m trying to ‘single source’ a form page which can be in edit mode or view mode. For various reasons, this isn’t using the ASP.Net FormView or DetailsView controls.

Since there is no way to disable a textbox without turning its contents gray (well, we could ‘eat’ all of the keystrokes into it, but that isn’t very elegant either) and disabling a dropdown list or listbox isn’t what we want, our first try was to duplicate all of the form input controls with a label and use CSS to select which ones are visible depending on the mode of the form. That works, but it’s ugly to edit and the code-behind has to populate both controls every time.

We could control the visibility in the code-behind to avoid filling both controls, but we still have to add them both to the form.

So I had the idea to use jQuery to swap out the input controls for <label>, <div>, or <span> elements. This works, to some extent, by creating the appropriate selectors and using the replace() jQuery method to swap out the elements dynamically.

The problem is that I not only need to copy the contents, but also the styles, attributes, and sizing of the original input controls (at this point we’re only talking about textboxes – we have a different solution for dropdown lists and listboxes).

Brute force should work – ‘backup’ all of the attributes of the input control, create the new ‘read only’ element, then replace the input control with the new element. What I’m looking for is something simpler.

Succinctly, using jQuery, what is the best way to replace a textbox with a label and have the label have the same contents and appear in the same location and style as the textbox?

Here is what I have so far:

$(':text').each( function() {     var oldClass = $(this).attr('class');     var oldId = $(this).attr('id');     var oldHeight = $(this).outerHeight();     var oldWidth = $(this).outerWidth();     var oldStyle = $(this).attr('style');     $(this).replaceWith('<div id='' + oldId + ''>' + $(this).val() + '</div>');     $('div#' + oldId).attr('class', oldClass);     $('div#' + oldId).attr('style', oldStyle);     $('div#' + oldId).width(oldWidth);     $('div#' + oldId).height(oldHeight);     $('div#' + oldId).css('display', 'inline-block'); }); 
  • 1 1 Answer
  • 0 Views
  • 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. 2026-05-10T21:05:23+00:00Added an answer on May 10, 2026 at 9:05 pm

    This may not suit your needs, but it’s a possibility.

    <input> and <textarea> tags support the read-only property. The behavior of read-only fields is slightly different than disabled. Here’s what the HTML 4.01 Recommendation says:

    When set, the readonly attribute has the following effects on an element:

    Read-only elements receive focus but cannot be modified by the user.
    Read-only elements are included in tabbing navigation.
    Read-only elements may be successful. ("Successful" means it will be submitted as a parameter.)

    Another key difference is that elements with this attribute can be styled however you like. (You could remove or change the borders and background for instance.) So instead of having to create new elements and copy attributes, you could merely add or remove the read-only attribute.

    You could then create a style for these fields "input[readonly] {}". Noting of course that popular versions of IE ignore the attribute selector in CSS. (So maybe just define a class that you add and remove.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 102k
  • Answers 102k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You've got a couple of options to "render" a graph… May 11, 2026 at 8:10 pm
  • Editorial Team
    Editorial Team added an answer From Douglass Crockford's style convention guide: (search for "invoked immediately")… May 11, 2026 at 8:10 pm
  • Editorial Team
    Editorial Team added an answer When you freeze a gem it copies it in to… May 11, 2026 at 8:10 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.