7

I have an <ol> (saved to Var1) and would like to be able to assign its last <li> element to a variable.

I tried doing the following:

Lastli = var1.lastChild

However, it doesn't seem to work.

I'd like to do this with vanilla JS, and no jQuery.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
ModS
  • 806
  • 2
  • 15
  • 28
  • 2
    Please provide some minimum code – willome Nov 09 '12 at 13:24
  • 1
    The reason you're not getting the last `
  • ` is probably that there is some white space after its closing `
  • ` tag. If you leave the closing tag out, you will be able to access it as `ul.lastChild` ;) – J. K. Nov 09 '12 at 13:28
  • Thanks every one! i forgot to define it as the first ul even though it was the only Ul. `::var x = document.getElementsByClassName('listSection')[0].getElementsByTagName('ul')[0]; var2 = x.lastChild; console.log(var2);` – ModS Nov 09 '12 at 13:36