admin管理员组

文章数量:1402783

I have the following code of jquery that gets the first word of the label.

var val = $(this).find('label').html(); // The value must be Graphic Designer
$(this).find('label').replaceWith('<input type=text value='+val+'); // The input's value is Graphic

Here is the jsfiddle

I have the following code of jquery that gets the first word of the label.

var val = $(this).find('label').html(); // The value must be Graphic Designer
$(this).find('label').replaceWith('<input type=text value='+val+'); // The input's value is Graphic

Here is the jsfiddle

Share Improve this question edited Jun 16, 2014 at 12:36 chridam 104k26 gold badges243 silver badges241 bronze badges asked Jun 16, 2014 at 12:33 deviloperdeviloper 7,24010 gold badges50 silver badges79 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 8

You forgot the html close tag character > and it's string ender ', and you should add " in your input. Should be:

'<input type=text size=40 value="'+val+'" />'
                                ^       ^

http://jsfiddle/vgNS8/

Build the input with jQuery instead:

$("#editInfoBtn").click(function(){
    $(".inline-update").each(function(){
        var val = $(this).find('label').html(),
                    // build the element here
                    $input = $('<input>',{attr:{'type':'text','size':40},val:val});
                // insert it here
        $(this).find('label').replaceWith($input);
    });
});

Chances are you're running into problem with the quotes.

本文标签: javascriptWhy html() and text() selects only the first wordStack Overflow