I want to know how to show form field editable on button click in PHP. Part of my view file:
<?php echo Form::label("email", "Email"); ?>
<?php echo Form::input("email", $userdetails->email,array('disabled' => 'disabled')); ?>
<br/>
<?php echo Form::label("description", "Description"); ?>
<?php echo Form::textarea("description", $userdetails->description,array('disabled' => 'disabled')); ?>
<br/>
<?php echo Form::submit("submit", "Submit"); ?>
<?php echo Form::close(); ?>
<a href="../index"><input type="button" name="edit" value="Edit" ></a>
In the page load field should be disabled
.
If the user clicks the Edit button the hidden field should turn to editable.
Mock Form
<form>
<input id="inp" type="text" disabled>
<input id="edit" type="button" value="Edit">
</form>
Javascript
var el = document.getElementById('edit');
el.addEventListener('click', function(){
document.getElementById('inp').disabled = false;
});
For usability sake, however, I suggest this:
var el = document.getElementById('edit');
var inp = document.getElementById('inp');
el.addEventListener('click', function(){
inp.disabled = false;
inp.focus(); // set the focus on the editable field
});
Edited: OP commented that several elements will need to be enabled on click.
var el = document.getElementById('edit');
var frm = document.getElementById('myform');
el.addEventListener('click', function(){
for(var i=0; i < frm.length; i++) {
frm.elements[i].disabled = false;
}
frm.elements[0].focus(); // put focus on the first element
});
You can use jquery to set the disabled
attribute to false
jQuery("input[name=edit]").click(function(){
jQuery("input[name=email],input[name=description]").attr("disabled",false);
});
input[name=email]
selects the email input field
input[name=description]
selects the description input field