javascript - jQuery replace only the text of a node, ignoring current internal nodes -
i have html:
<label>search: <input id="search-box" type="text"/> </label>
if want change words "search:" "quick search:", how can select "search:" text using jquery/js?
i have tried html()
, text()
, , other various jquery methods. goal not have copy input box , append after. possible?
p.s. know can use prepend()
function, goal here replace text entirely, , i'd know how in future well.
thanks bunch.
if have access html i'd wrap text in span
can address easily. however, have far can iterate on nodes in element , change textcontent
of first text node (nodetype==3
):
$(function(){ $("label").contents().each(function() { if(this.nodetype == 3){ this.textcontent = "quick search: "; return false; } }) });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label>search: <input id="search-box" type="text"/> </label>
Comments
Post a Comment