Is there a way to chain this jQuery expression or improve it? All i really want to do is change “my text” to “new text”.
HTML:
<div id="myDiv">
my text
<span>button 1</span>
<span>button 2</span>
</div>
Javascript:
var element = $("#myDiv");
var buttons = element.children().detach();
element.text("new Text");
element.append(buttons);
Should work fine! You can always chain methods, that use the same selector.