admin管理员组文章数量:1022467
Given:
let mystr = "<input class=\"text-box single-line\" id=\"item_Name\" name=\"item.Name\" type=\"text\" value=\"Luis Tiant\">";
Given:
let mystr = "<input class=\"text-box single-line\" id=\"item_Name\" name=\"item.Name\" type=\"text\" value=\"Luis Tiant\">";
I'd like to remove the text in the value param "Luis Tiant"
using JS.
To be clear: I want to change value="Luis Tiant"
to value=""
in the string itself. This is a string not yet a DOM element. After I remove the value then I'll add it to the DOM.
-
document.getElementById("item_Name").value = "";
– PajLe Commented May 22, 2020 at 13:54 - 1 Does this answer your question? How to remove/change an input value using javascript – Flori Bruci Commented May 22, 2020 at 13:55
- thank you for the reply. This won't work. I apologized for not be more clear. mystr is a string and I want to change value="Luis Tiant" to value="" in the string – Carlos Vasquez Commented May 22, 2020 at 13:58
- @CarlosVasquez I've updated my answer below to meet your new requirements. – pretzelhammer Commented May 22, 2020 at 14:12
4 Answers
Reset to default 1Get the input
element and set its value
to ''
(empty string).
Example below clears the input value after 2 seconds, so you can see it in action:
setTimeout(() => {
document.querySelector('input').value = '';
}, 2000);
<input class="text-box single-line" id="item_Name" name="item.Name" type="text" value="Luis Tiant">
Update
Question above has been clarified. If you'd like to replace the value
attribute in the string itself you can acplish that using regex and the replace
method like so:
let string = "<input class=\"text-box single-line\" id=\"item_Name\" name=\"item.Name\" type=\"text\" value=\"Luis Tiant\">";
console.log(string);
let newString = string.replace(/value=\".*\"/, "value=\"\"");
console.log(newString);
By initializing the ID
you can do this as well.
document.getElementById("item_Name").value = "Your new Value Here";
Instead of setting a variable equal to a string of html, then using string manipulation to change the element attributes, I'd suggest using the Document.createElement() method and related APIs to programmatically create the html element. Then you'll have access to methods like Element.removeAttribute()
let mystr = "<input class="text-box single-line" id="item_Name" name="item.Name" type="text" value="Luis Tiant">"
var res = mystr.match(/value=\".*\"/g);
var str = mystr.replace(res, 'value=""');
Given:
let mystr = "<input class=\"text-box single-line\" id=\"item_Name\" name=\"item.Name\" type=\"text\" value=\"Luis Tiant\">";
Given:
let mystr = "<input class=\"text-box single-line\" id=\"item_Name\" name=\"item.Name\" type=\"text\" value=\"Luis Tiant\">";
I'd like to remove the text in the value param "Luis Tiant"
using JS.
To be clear: I want to change value="Luis Tiant"
to value=""
in the string itself. This is a string not yet a DOM element. After I remove the value then I'll add it to the DOM.
-
document.getElementById("item_Name").value = "";
– PajLe Commented May 22, 2020 at 13:54 - 1 Does this answer your question? How to remove/change an input value using javascript – Flori Bruci Commented May 22, 2020 at 13:55
- thank you for the reply. This won't work. I apologized for not be more clear. mystr is a string and I want to change value="Luis Tiant" to value="" in the string – Carlos Vasquez Commented May 22, 2020 at 13:58
- @CarlosVasquez I've updated my answer below to meet your new requirements. – pretzelhammer Commented May 22, 2020 at 14:12
4 Answers
Reset to default 1Get the input
element and set its value
to ''
(empty string).
Example below clears the input value after 2 seconds, so you can see it in action:
setTimeout(() => {
document.querySelector('input').value = '';
}, 2000);
<input class="text-box single-line" id="item_Name" name="item.Name" type="text" value="Luis Tiant">
Update
Question above has been clarified. If you'd like to replace the value
attribute in the string itself you can acplish that using regex and the replace
method like so:
let string = "<input class=\"text-box single-line\" id=\"item_Name\" name=\"item.Name\" type=\"text\" value=\"Luis Tiant\">";
console.log(string);
let newString = string.replace(/value=\".*\"/, "value=\"\"");
console.log(newString);
By initializing the ID
you can do this as well.
document.getElementById("item_Name").value = "Your new Value Here";
Instead of setting a variable equal to a string of html, then using string manipulation to change the element attributes, I'd suggest using the Document.createElement() method and related APIs to programmatically create the html element. Then you'll have access to methods like Element.removeAttribute()
let mystr = "<input class="text-box single-line" id="item_Name" name="item.Name" type="text" value="Luis Tiant">"
var res = mystr.match(/value=\".*\"/g);
var str = mystr.replace(res, 'value=""');
本文标签: javascriptHow to replace the value attribute in an HTML stringStack Overflow
版权声明:本文标题:javascript - How to replace the value attribute in an HTML string? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745572931a2156845.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论