onClick or click , javascript and html

Hey guys I am writing a website and I essentially pull info from a database and then send that data to the client where I make a list of those items on the site by using javascript to create a new li for every item. I was wondering how I could make it so each item in the list can be clicked on and I could open a page that displays more information about that respective item?

If I had a java script function that is called in other words I would need a way for the onclick or click function to know what element it was called from.

so are you saying you would like to link a site? just like the html href but in JS?

Are you using jQuery or just javascript? If you're using jQuery check out .on with event delegation.

My plan so far is to change my javascript code that add elements to the list to also add an tormenting id and each id will have a number that will correspond to a row in the database. Once I send that to the server in a post request I can load up a page that will display more information about what I clicked on and the server can serve data that correlates to that id number I passed.

I think I understand what you're asking now. I would pass the id's to your view with your initial query and set each li with an id of that item's id. When a link is clicked you can grab the value of it's id and pass it as a query string to the page you want to load.

1 Like

use data-id, as you'll be able to manipulate this in the DOM after you dynamically pull the stuff from the database, that or use AngularJS to update object model as document changes.