Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to append an element as a first child in jquery

Tags:

jquery

lets say i have html something like this..

<ul id="leftNavigation">   <table id="leftNavTable"><tr><td>blablabla</td></tr><table>   <li>1</li>   <li>2</li> </ul> 

what i want to do is when i click on any li, then that table should get hide and another table should get appended in place of that table.lets say that table is

<table id="Selected"><tr><td>blablabla</td></tr><table> 

how to do it in jQuery.

i tried in this way but its adding as a last child of ul.

$('#leftNavigation').append('<table id="Selected"><tr><td>blablabla</td></tr><table>') 

Thanks in advance!!!!

like image 768
Vivek Avatar asked Jan 28 '11 10:01

Vivek


People also ask

How do you insert an element to your first child?

To insert element as a first child using jQuery, use the prepend() method. The prepend( content ) method prepends content to the inside of every matched element.

What is first child in jQuery?

Definition and Usage. The :first-child selector selects all elements that are the first child of their parent. Tip: Use the :last-child selector to select elements that are the last child of their parent.

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.


1 Answers

Use .prepend() or .prependTo() instead.

Check jQuery documentation.

Additional notes

But based on what you're trying to accomplish you should maybe rather use DOM replacement functionality.

Beware

Adding anything else than LI inside an UL or OL is invalid HTML.

I suggest you add an additional LI, give it some ID or CSS class and then manipulate its content.

like image 149
Robert Koritnik Avatar answered Oct 20 '22 15:10

Robert Koritnik