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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:29:00+00:00 2026-06-04T16:29:00+00:00

In general: What I want is to have PHP output an unordered list with

  • 0

In general: What I want is to have PHP output an unordered list with books. When the user clicks on one of the titles, the details will be pulled from the DB and inserted into the page using AJAX.

I can’t seem to figure out how to pull the title value from the < li > using javascript, so I can pass that to AJAX.

(To simplify the example code, I left out the AJAX part and am using an alert instead.)

<html>
<head>
<script type="text/javascript">

window.onload = function()
{

      var shelve = document.getElementById( 'books' );
      var books = shelve.getElementsByTagName( 'li' );
      for( var i=0; i < books.length; i++ ){
         books[i].onclick = function(){
        alert(books[i].title); //I know this doesn't work
         };
      }
}

</script>
</head>
<body>

<div id="txtHint"><b>Book info will be listed here using AJAX.</b></div>

<?php show_allBooks(); ?>

</body>
</html>

<?php

function show_allBooks(){

    db_connect();
    $query = "Select * from tblBooks order by title";
    $result = mysql_query($query);
    echo "<ul id='books'>";
    while ($row = mysql_fetch_array($result))
    {
        echo "<li title='" . $row['id'] . "'>" . $row['title'] . "</li>";
    }
    echo "</ul>";
    db_close();
}
?>

I tried fooling around with variations of the following code, but this did not seem to work either.

books[i].getAttribute("title")

Can anyone point me in the right direction? Any help would be greatly appreciated.

  • 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-06-04T16:29:01+00:00Added an answer on June 4, 2026 at 4:29 pm

    By the time your onclick handler gets called, i has been incremented to books.length. Instead, you can reference this to get the element.

    var shelve = document.getElementById('books');
    var books = shelve.getElementsByTagName('li');
    for(var i = 0; i < books.length; i++) {
        books[i].onclick = function () {
            alert(this.title); 
        };
    }
    

    http://jsfiddle.net/gilly3/cPMAU/

    However, if you do need to know the value of i in the click handler, you can do it by creating your handler in a factory, that is a function that returns a function:

    function createBookHandler(books, i) {
        return function() {
            alert(books[i].title);
        };
    }
    var shelve = document.getElementById('books');
    var books = shelve.getElementsByTagName('li');
    for(var i = 0; i < books.length; i++) {
        books[i].onclick = createBookHandler(books, i);
    }
    

    http://jsfiddle.net/gilly3/cPMAU/1/

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

Sidebar

Related Questions

I have one very general object that I want to map to a destination
We have an application that we want to make available to the general internet-using
I want to have a record editor in PHP and I want to be
I'm new to PHP and web development in general, but have been a programmer
I have a dynamic php (Yii framework based) site. User has to login to
I'm new to cookies and (PHP in general actually) and I want to implement
I have a query string, like: n1=v1&n2=v2&n3=v3 etc I just want a php function
Hi have some forms that I want to use some basic php validation (regular
I have recently written a socket server in PHP that will be handling communication
Ok, this is a hard one. I have three things: An iPhone app PHP-mySQL

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.