Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
James Barrett
13,253 PointsHow do you getElementById from a PHP functions page?
Hi there,
I am currently applying some AJAX to my project and I am puzzled as to how I would access a div which has been echod on a PHP functions page (back to an existing JS function):
PHP Code Snippet:
function getAvailableLiftsHTML($object) {
$html = "<ul>
<li id='liftid'>"."ID: ".$object['L_ID']."</li>
foreach($findLiftQuery as $search) {
echo getAvailableLiftsHTML($search);
echo "<button class='lift' id='joinlift'>Join lift</button>";
JavaScript Code Snippet:
function joinLift() {
var joinlift = document.getElementById("joinlift");
var liftid = document.getElementById("liftid");
How do I access joinlift and liftid from a javascript functions page?
Thanks, James.
1 Answer
Seth Kroger
56,416 PointsI think the problem is that you're using these two ids in a foreach loop, which means you'll be repeating the id's on the page for every item of the list. But id's are only supposed to be used once in a page. Perhaps class would be better than id?
Stephan Olsen
6,650 PointsStephan Olsen
6,650 PointsWhat happens when you try to do it this way? I'm by no means an expert in either php or JavaScript, but you seem to be using the getElementById correctly.
Did you remember to close the function correctly in your original code? It seems to be missing in the snippet. Also are you calling the function after you've declared it?