Jump to content

Hello,

 

So this is the Html

 

<ul id="theList">
  <div id="sdas">
    <div id="adsdsa">

I want to append a element to the last div, the ID's are random so I can't use that one.

 

How can I get the last div element using the ID of UL? I know this has something to do with siblings.

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/909763-jquery-select-element-help/
Share on other sites

Link to post
Share on other sites

13 minutes ago, HarryNyquist said:

$("#theList div div:last-child")

That should work unless I've not understood you properly.

 

https://api.jquery.com/last-child-selector/

Yea this seems to work, but I'm getting a really odd effect here, the element is added to the last div, ok thats what I want. But it's also added to the new element's div, and it's sub divs. What? :P

Back-end developer, electronics "hacker"

Link to post
Share on other sites

Going directly by the id of the last element (using console) it got added as I expected it to. Now if I do this on page load (I did div div as I do not have more than 1 div under it)

$("#theList div div").append("<p>Test</p>");

it adds it to all the divs under the

<ul id="theList">
  <div id="sdas">
    <div id="adsdsa">
      <li>
        <div>
        <!-- My added element -->
        </div>
        <div>
        <!-- My added element -->
        </div>
      </li>
      <!-- My added element -->

Why does it do this?

Back-end developer, electronics "hacker"

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×