Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery div to textbox onclick and back to div

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 :)

like image 914
Rob Avatar asked Dec 08 '22 15:12

Rob


1 Answers

I 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
    });
});
like image 146
cfs Avatar answered Dec 29 '22 02:12

cfs