jQuery setting input value attribute using .html() not working

Robert Rocha

I want to be able to set the contents of the value attribute in the input element but have not been successful doing this.

HTML:

<div class='photo-info'>
    Photo Name : <span class='photo-name'><?php echo $title; ?></span>
    <span class='title-edit'><a href='#'>Edit</a></span>
</div>

jQuery:

// show editable area for title
    $('.title-edit').on('click', function () {
        var $titleElm = $(this).prev();
        var text = $titleElm.text();
        $titleElm.html("<input type='text'>").val('tom');
    })

Result:

https://jsfiddle.net/fh7t9qwd/1/

BenM

Add find('input') after you update the HTML:

$titleElm.html("<input type='text'>").find('input').val('tom');

jsFiddle Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Setting new value for an attribute using jQuery

html attribute of JQuery not working

Angular HTML Forms - value attribute not working in input field

Modifying "value" attribute of "text" input not working using JavaScript

Setting value of 'date-time local' input box using JQuery, normal datetime selection form drop down is not working?

Change the value of span using Javascript or Jquery using <input> or text attribute

html input field value setter not working properly in chrome and safari using jquery

getting input dropdown text value based on data attribute using jquery

get input value using data-attribute with jquery

setting input value using javascript

Setting a dropdown selected value when generating a html table using jquery

Setting the attribute of an HTML tag with EJS or jQuery

Setting date input value with moment not working

Jquery append data attribute value dynamically using html

Change value of class attribute in html using jquery or javascript

How to get the value of custom HTML attribute using JQuery?

Setting attribute value of an element in camelCase using a directive

setting a div with an hidden data attribute using jquery

html input pattern attribute not working correctly

Setting value to null using jQuery

getting html attribute value with jquery

Setting the value of a select2 input with jquery

setting value of an input with jQuery doesn't work

setting a text input value via jquery mobile

jQuery - Setting number input value as float in Firefox

Setting value of input from a jQuery script

Setting input value to a var through jquery

my jQuery code for setting value in combobox , is not working

getting an attribute value using jquery