javascript - jQuery text to input? - Stack Overflow

I have a simple layout with a textblock and one <a>, like:text1 editWhen I click on the <a>

I have a simple layout with a textblock and one <a>, like:

text1 edit

When I click on the <a>, I want this textblock to bee text input. How is that possible with jQuery?

I have a simple layout with a textblock and one <a>, like:

text1 edit

When I click on the <a>, I want this textblock to bee text input. How is that possible with jQuery?

Share Improve this question edited Oct 14, 2011 at 10:34 Peter-Paul van Gemerden 7,00932 silver badges37 bronze badges asked Oct 14, 2011 at 8:41 Ilya SmaginIlya Smagin 6,15211 gold badges42 silver badges57 bronze badges 2
  • I have answered a similar question, take a look to: stackoverflow./a/28360984/442883 – rsa Commented Feb 6, 2015 at 7:57
  • I have answered a similar question, take a look to: stackoverflow./a/28360984/442883 – rsa Commented Feb 6, 2015 at 7:58
Add a ment  | 

4 Answers 4

Reset to default 3

Use jQuery replaceWith():

$(document).ready(function(){
    $("a").click(function(e){
        e.preventDefault();
        $(this).parent().replaceWith("<input />");
    });
});

http://jsfiddle/Dqkyg/1/


Furthermore, you can fill the textbox with the text outside the link like so:

$(document).ready(function(){
    $("a").click(function(e){
        e.preventDefault();
        var txt = $(".text").html();
        $(".text").replaceWith("<input value='" + txt + "' />");
    });
});

http://jsfiddle/Dqkyg/5/


The following will replace the edit with Save & Cancel options:

<div>
    <span class="text">testing</span> <a href='#'>test</a>
</div>

$(document).ready(function(){
    $("a").click(function(e){
        e.preventDefault();
        var txt = $(".text").html();
        $(".text").replaceWith("<input value='" + txt + "' />");
        $(this).replaceWith("<a href='#'>Save</a> | <a href='#'>Cancel</a>");
    });
});

http://jsfiddle/Dqkyg/6/

There are plenty of plugins that you can use... No need to reinvent the wheel

http://www.appelsiini/projects/jeditable

Hmm ... ur a bit unclear... but http://jsfiddle/mMYrf/4/

<div class="dataGrid">
    <span>test</span>
    <a href="#">edit</a>
</div>

And

$('.dataGrid a').click( function(e) {
        e.preventDefault();

        var EditGrid = $(this).parent().children('span');
        var DataTxt = EditGrid.text();
        var InputHtml = '<input type=text" value="'+DataTxt +'" />';

        $(this).text('Save?');
        EditGrid.html(InputHtml);
});
<div class="editable">This is the content.</div>
<script type="text/javascript">
    $(function() {
        $('.editable').each(function() {
            var a = $('<a href="#">edit</a>');
            (function(link, parent) {
                link.click(function() {
                    link.remove();
                    var textField = $('<textarea/>').val(parent.html());
                    parent.replaceWith(textField);
                    delete parent, link;
                });
            })(a, $(this));
            $(this).append(" ").append(a);
        });
    });
</script>

Working example: http://jsfiddle/BnH29/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744763126a4592290.html

相关推荐

  • javascript - jQuery text to input? - Stack Overflow

    I have a simple layout with a textblock and one <a>, like:text1 editWhen I click on the <a>

    18小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信