Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

question regarding jQuery selector

<div id="foo">yyy<span>xxx</span></div>

I have the the above structure for my html. I want to insert some content at yyy position. Can you let me know what will be the selector for it? I would pass that selector to somefunction and that function will do $(selector).html('content')

like image 504
Rocky Singh Avatar asked Feb 23 '26 17:02

Rocky Singh


2 Answers

var s = $('#foo span');
$('#foo').text("hello").append(s);

Demo: http://jsfiddle.net/uTNTF/

Or, if updating the HTML is an option, then simply wrapping yyy in a <span> will make your life a lot simpler:

$('#foo span:first-child').text("hello");

Demo: http://jsfiddle.net/uTNTF/1/

like image 166
Shawn Chin Avatar answered Feb 25 '26 07:02

Shawn Chin


You can use jQuery .prepend() function (insert content, specified by the parameter, to the beginning of each element in the set of matched elements):

$('#foo').prepend('some content');
like image 25
Hck Avatar answered Feb 25 '26 08:02

Hck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!