Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement read / edit mode in form

Tags:

html

jquery

css

I am implementing a form that has two states. Read only mode where a user can read information from labels with information like name, age, address and so on.. In addition to this I want an edit mode where the user can edit the information.

Standard view should be read-only mode, and when the user clicks edit I want the labels to change to textboxes and be editable.

Whats the best way to implement this with the use of html, css and jquery?

like image 228
ffffff01 Avatar asked Mar 16 '26 02:03

ffffff01


1 Answers

What you're looking for is called "in-place editing". Don't waste time re-inventing the wheel. :)


But just for a quick idea I'll post a short snippet to get you started -

  1. Always render in edit mode by default
  2. Make readonly if required - as follows

 <form data-mode="read">
    <input value="Hello" />
 </form>

if($('form').data('mode') == 'read'){   //remove fields and add text
  $('form').find(':input').each(function(){
     $(this).replaceWith($('<span>' + $(this).val() + '</span>');
  });
 }

Note: Instead of replacing with labels you can disable them instead using .prop('disabled', true).

like image 71
Robin Maben Avatar answered Mar 18 '26 16:03

Robin Maben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!