• email1
  • email2
    All geek questions in one place

    RemoveChild on li inside ul inside div with id?

    I have the following code on a web page:

    <div id="emails">
    <ul>
    <li>email1</li>
    <li>email2</li>
    </ul>
    </div>
    
    <a href="#" onClick="deleteEmail()">click</a>
    

    and I want to delete the first email I tried with this:

    function deleteEmail(){
        var ul = document.getElementById('emails').getElementsByTagName('ul');
        ul.removeChild(ul.childNodes[0]);
    }
    

    im kinda new for the Javascript DOM, so if there is a better way to do this, please let me know.

    Note. I would like to do this without any js frameworks.

    +3
    javascript dom
    Prozaker Jun 14 '10 at 22:50
    source share
    5 answers

    The most correct:

    var ul = document.getElementById('emails').getElementsByTagName('ul')[0];
    var li = ul.getElementsByTagName('li')[0];
    ul.removeChild(li)
    
    +3
    Aaron harun Jun 14 '10 at 22:59
    source share

    1) More correct line:

    var ul = document.getElementById('emails').getElementsByTagName('ul')[0];
    

    2) Note that in "ul.childNodes [i]" I will be 0 for spaces, 1 for <li>email1</li>, 2 for space, etc. You should use ul.getElementsByTagName ('li') [i] if you're only insterested in <li>s.

    +3
    Pavel Strakhov 14 . '10 23:00

    DOM node , , , , , , . <li> <ul> 1.

    The DOM for your `email 'div looks like this:

    DIV
      text( whitespace )
      UL
        text ( whitespace )
        LI 
          text (email1)
        text ( whitespace )
        LI
          text (email2)
        text ( whitespace )
      text (whitespace)
    

    That being said, the easiest way is to find <li>which one you want to remove and then delete.

    var toRemove = document.
          getElementById('emails').
          getElementsByTagName('ul')[0]. 
          getElementsByTagName('li')[0];
    
    toRemove.parentNode.removeChild( toRemove );
    
    +1
    jimr Jun 14 '10 at 23:55
    source share
    <div id="emails">
    <ul>
    <li>email1</li>
    <li>email2</li>
    </ul>
    </div>
    
    <a href="#" onClick="deleteEmail()">click</a> 
    <!--note that I made it so you actually invoke deleteEmail -->
    

    and I want to delete the first email I tried with this:

    function deleteEmail(){
        //I believe you meant emails =/
        var ul = document.getElementById('emails').getElementsByTagName('ul');
        ul.removeChild(ul.getElementsByTagName("li")[0]);
    }
    
    +1
    Warty Jun 15 '10 at 0:16
    source share

    If you change the line "email #" to a link ... something like this:

    <a href="" onClick="removeElement(this.parentNode);return false">email1</a>

    With a similar function.

    function removeElement(childElm) {
      var parentElm = childElm.parentNode;
      parentElm.removeChild(childElm);
    }
    

    Although you can use removeElement () without onClick, but I just like the simplicity that it allows.

    +1
    Brook julias Jun 15 '10 at 0:27
    source share

    Source: https://habr.com/ru/post/1750021/

    More articles:

    • Binding to DependencyProperty WPF Control in WinForms - c #
    • Display vectors - c #
    • Failed to connect the "AdventureWorks2008" trial database to a named instance in SQL Server 2008 - sql-server-2008
    • С++: отслеживать функцию времени - c++
    • How to Assign a TreeNodes Numbering Scheme Based on Position - c #
    • Пакет не найден; Javac - java
    • https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1750023/browser-connects-to-wcf-service-but-not-my-wcf-client-what-can-be-the-reason&usg=ALkJrhiUVU_FavaAG0bzmzmc_pPmwTObiQ
    • Получение сбоя после выбора изображений из UIImagePickerController (Связано с утечкой памяти?) - memory-management
    • What does setting the GL color do before performing the texture mapping operation? - opengl
    • С++: Неизвестный размер указателя при прямом объявлении (ошибка C2036) - c++

    All Articles

    Geek Questions | 2019