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 7678971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:46:08+00:00 2026-05-31T17:46:08+00:00

I have this PHP code in a loop for every post on the admin

  • 0

I have this PHP code in a loop for every post on the admin page of a blog:

echo '<a class="delete_link" id="delete_link_'.$row['rowid'].'">Delete</a>';

So it outputs:

<a class="delete_link" id="delete_link_1">Delete</a>
<a class="delete_link" id="delete_link_2">Delete</a>
<a class="delete_link" id="delete_link_3">Delete</a>

and so on…

By clicking on these links, I would like the browser to send a DELETE request through XHR, but I don’t know how to set up the events an unobtrusive way. My example doesn’t work, as sendDeleteRequest() is fired with the last added id, no matter which link I click on:

function sendDeleteRequest(id) {
    // I only put a simple alert in this function for now.
    alert("Id of firing element: " + id);
}

onload = function() {
    var deleteLinks = document.getElementsByClassName("delete_link");
    for (var node in deleteLinks) {
        deleteLinks[node].onclick = function() {
            sendDeleteRequest(deleteLinks[node].getAttribute("id"));
        }
    }
}

What’s wrong with this code? Anything else I should pay attention to? I’ve tried the same with addEventListener() with no success. I still use XHTML 1.0 and I can’t use jQuery in this project.

  • 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-31T17:46:10+00:00Added an answer on May 31, 2026 at 5:46 pm

    You could use something like this:

    var deleteLinks = document.getElementsByClassName("delete_link");
    for (var i = 0; i < deleteLinks.length; i++) {
        deleteLinks[i].onclick = function() {
            sendDeleteRequest(this.id);
        }
    }
    

    Note, it’s not wise to iterate an array with for (node in deleteLinks) because that will include enumerable properties in the iteration in addition to the array elements. That type of iteration should only be used for iterating all properties of an object.

    The main change I made in your code was to use a reference to this to get the clicked on id value. Your way of doing it caused problems because the for loop and the value of node had already run to completion when the click happened sometime later so the value of node was not what you wanted it to be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this PHP code echo '<a href=# onclick=updateByQuery(\'Layer3\', ' . json_encode($query) . ');>Link
I have this php code, with which I am trying to generate a popup
I have this php code $jsonArray = array(); $sql = SELECT ID,CLIENT FROM PLD_SERVERS;
i have this php code and keep on getting errors upon fixing each error.
I have this php code $filename = verbs.php; // http://alylores.x10.mx/vega/verbs2.php $handle = fopen($filename, r);
i have this PHP code, and a DataBase with Question, answer1, answer2, Question_id well,
Say i have this PHP code: $FooBar = a string; i then need a
I have this piece of php - mysql code: When a user tagged in
I found this PHP code in an app I have to modify... $links =
I have a PHP loop like this <?php foreach ($products as $product) { ?>

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.