Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6195349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:27:13+00:00 2026-05-24T03:27:13+00:00

Please no jQuery code, as I want to learn javascript first and foremost. I

  • 0

Please no jQuery code, as I want to learn javascript first and foremost.

I am just trying something out and I have a button in my html page that dynamically adds li items to a ul. Something to this effect:

HTML:

<ul id="myList">
</ul>
<input id="myButton" value="Click Me" type="submit" onclick="addItem();"></input>

And the JS for addItem():

function addItem()
 {
  var l = document.getElementById("myList");  
  var today = new Date();
  var day2 = new Date();
  day2.setDate(today.getDate() + 30);
  var count = 1;

    while(today.valueOf() < day2.valueOf())
           {
            if(today.getDay() == 0)
                 //is it a sunday -- only add sundays...
                {
            var li = document.createElement('li');
            li.setAttribute('id', ['liID' + count]);  //set the id attribute
                    var month = today.getMonth();
                    var day = today.getDate();
                    var year = today.getFullYear();
                    var theDay = month + '/' + day + '/' + year + ' (Sunday)';
            li.innerHTML = theDay;
            l.appendChild(li);     //add the li to the ul
                }
            today.setDate(today.getDate() + 1) //increment the day
        count++;       
           }
 }

This is nice however, next to each line item I would like to also add a hyperlink that would say ‘Remove’ so that if a user clicks on it, it then would remove that single li. Do I just do a document.createElement('a') ? and set its attribute via .setAttribute. But then how do I trigger the delete for that specific li? Here’s my try at it:

Edit

 function addItem()
 {
  var l = document.getElementById("myList");  
  var today = new Date();
  var day2 = new Date();
  day2.setDate(today.getDate() + 30);
  var count = 1;


    while(today.valueOf() < day2.valueOf())
           {
            if(today.getDay() == 0)
                {
            var li = document.createElement('li');
            li.setAttribute('id', ['liID' + count]);
                    var month = today.getMonth();
                    var day = today.getDate();
                    var year = today.getFullYear();
                    var theDay = month + '/' + day + '/' + year + ' (Sunday)';
            li.innerHTML = theDay;
            l.appendChild(li);

            var a = document.createElement('a');
            a.setAttribute('href', '#');
            a.innerHTML = "Remove";
            a.onclick = function(e) {  
                                                var liNode;   
                        if (!e) var e = window.event;   
                        if (e.target) liNode = e.target;   
                        else if (e.srcElement) liNode = e.srcElement;   
                        if (liNode.nodeType == 3) 
                        { // defeat Safari bug        
                          liNode = liNode.parentNode;       
                        }   
                        // l refers to ul which you've an instance of already   
                        l.removeChild(liNode);
                                            }

                    li.appendChild(a);
                }
            today.setDate(today.getDate() + 1)
        count++;
           }
 }

But when I click the href link it does not remove anything…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:27:13+00:00Added an answer on May 24, 2026 at 3:27 am

    Yes you can add a using var anchor = document.createElement('a');. And yes, you can add attribute by using setAttribute or just by using anchor.href = 'http://yourpath.com';. Then you can add event listener using following code:

    anchor.onclick = function(e) {
      // crossbrowser event handling
      e = e || window.event;
      var tagret = e.target || e.srcElement;
      // assuming you have appended anchor to li
      var li = tagret.parentNode;
      var ul = li.parentNode;
      ul.removeChild(li);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please bear with me, I'm just learning C++. I'm trying to write my header
I have this little jQuery plugin: <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd>
I have been looking at the YouTube Insight function and want to learn how
Please see the jQuery code below, it used to paginate some search results paginate:
I have a whole range of jQuery code, how do I stop users from
I have this javascript function I am trying to understand. I don't know if
I'm having trouble with some jQuery stuff, please help me out. Below is my
Lately I've been writing some JS code using jQuery and JavaScript as it is
I want to convert following Jquery code to use $.ajax intead of $.getJSON, what
Please help me, I want to display the position of a radio button when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.