简体   繁体   中英

jQuery replace input placeholder using method replaceWith()

I have an input like this:

<input id="inputId" placeholder="Old Placeholder Here"/>

I want to replace placeholder using .replaceWith() method. How can I achieve that?

Assuming that you mean you are trying to change the value of the placeholder attribute, then you don't want to use replaceWith() as that is designed to replace an entire element with another.

Instead you should use prop() :

$('#inputId').prop('placeholder', 'New placeholder value...');

The .replaceWith() method is to replace DOM elements, not attributes in elements. You should use the .attr() or .prop() methods for that.

$('input').attr('placeholder', 'new placeholder...');

jsFidle example: http://jsfiddle.net/pwdesfwq/

To change attributes of an element use $("<element>").attr("<attribute>", "<value>"); In your case, it is

$("input").attr("placeholder", "Placeholder value");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM