If you have been on the new PHP myadmin you can click on a field and edit it then when you click away it changes back to a div, I thought this could be found on the web easy but aprently not, so I tryed to make it and I'm not very good with javascript so I failed. Heres what i got so far.
HTML:
<td id="td_1"><input type="hidden" value="0" />0</td>
Javascipt:
$("#td_1").click(function() {
$input = $("#td_1");
$field = $('<input type="text" id="txt_1" maxlength="1" size="1"/>').attr({
id: $input.id,
name: $input.name,
value: $input.val()
});
$input.after($field).remove();
});
It adds the textbox but does not add the value to it, and stuck on how to change it back
Thanks for any help :)
If you have been on the new PHP myadmin you can click on a field and edit it then when you click away it changes back to a div, I thought this could be found on the web easy but aprently not, so I tryed to make it and I'm not very good with javascript so I failed. Heres what i got so far.
HTML:
<td id="td_1"><input type="hidden" value="0" />0</td>
Javascipt:
$("#td_1").click(function() {
$input = $("#td_1");
$field = $('<input type="text" id="txt_1" maxlength="1" size="1"/>').attr({
id: $input.id,
name: $input.name,
value: $input.val()
});
$input.after($field).remove();
});
It adds the textbox but does not add the value to it, and stuck on how to change it back
Thanks for any help :)
Share Improve this question asked May 24, 2013 at 21:08 RobRob 3122 gold badges3 silver badges15 bronze badges 2 |3 Answers
Reset to default 9I would have a hidden textbox that you show when you click on the text value. Working example
HTML
<td id="td_1"><input id="txtBox" type="textbox" value="0" style="display:none;" /><span id="txtBoxValue">0</span></td>
jQuery
$(function() {
$('#txtBoxValue').on('click', function() {
$(this).hide(); //hide text
$('#txtBox').show(); //show textbox
});
$('#txtBox').on('blur', function() {
var that = $(this);
$('#txtBoxValue').text(that.val()).show(); //updated text value and show text
that.hide(); //hide textbox
});
});
Try this fiddle, it works on multiple span
's and input
's.
<div class="editDIV">
<span class="editESPAN" style="display:block;">asd</span>
<input class="editINPUT" style="display:none;" type="text"/>
</div>
<div class="editDIV">
<span class="editESPAN" style="display:block;">asd</span>
<input class="editINPUT" style="display:none;" type="text"/>
</div>
<script type="text/javascript">
$(document).ready(function () {
$(".editDIV").click(function(){
$(this).find("span")[0].style.display="none";
$(this).find("input")[0].style.display="block";
$(this).find("input")[0].focus();
});
$(".editINPUT").blur(function(){
$(this)[0].style.display="none";
$(this).prev()[0].innerText=$(this)[0].value;
$(this).prev().show();
});
});
</script>
$("#td_1").click(function() {
$input = $("#td_1");
$field = $('<input type="text" id="txt_1" maxlength="1" size="1"/>').attr({
id: $input.id,
name: $input.name,
value: $input.text()
});
$input.after($field).remove();
});
contenteditable
attribute in HTML5. It may help:<div id="example-one" contenteditable="true">CLICK AND EDIT ME</div>
. – acdcjunior Commented May 24, 2013 at 21:11$("#myDiv").html="<textarea id="myTextbox">"+$("#myDiv").text+"</textarea>";
– David Starkey Commented May 24, 2013 at 21:12