0

I am trying to get desks highlighted that are available based off of a form that asks for the the day, time start and time end. I am able to echo out all the desks that are available, but I cant get the jquery to work with it.

foreach ($allData as $desk => $id){

  foreach ($id as $computer){?>
      <div id="<?php echo $desk?>"></div><?php 
    }

}

<style>
  .availableDesk{
background: #000000
  }
</style>

<script>
  $(document).ready(function() {
  jQuery( " <?php echo $desk ?> " ), addClass('availableDesk') ; 
})
</script>

DESKS:

<ul class="tabs">
<li id="1A"><a href="#1A"><div id="ddesk"></div></a></li>
    <li id="1B"><a href="#1B"><div id="ddesk"></div></a></li>
    <li id="1C"><a href="#1C"><div id="ddesk"></div></a></li>
</ul>

2 Answers 2

2

You're missing the ?> at the end of your PHP block.

<?php
foreach ($allData as $desk => $id){

  foreach ($id as $computer){?>
      <div id="<?php echo $desk?>"></div><?php 
    }

} // You need a "?>" here
?>

There are a few things wrong with your jQuery code. First off, if $desk is an ID, you need to do $('#ID'). Second, you have a comma before addClass instead of a period.

jQuery("#<?php echo $desk ?>").addClass('availableDesk');

P.S. HTML ID's aren't supposed to start with a number. Also, you cannot have multiple elements with the same ID, I suggest you use classes instead.

Sign up to request clarification or add additional context in comments.

2 Comments

That fixed all of my problems, but not it is only highlighting one of the desks - should I put the jquery in my foreach statement?
@Jamie: You don't need jQuery to add the availableDesk class to your div, just add that in the HTML in the loop. <div id="<?php echo $desk?>" class="availableDesk"></div>. Remember that IDs need to be unique.
2

I can see a couple of potential issues here...

jQuery( "<?php echo $desk ?>" ), addClass('availableDesk') ;

Syntactically, I think this should be:

jQuery("#<?php echo $desk ?>").addClass('availableDesk');

Note the # in the selector, this tells jQuery that you are looking for an id. The addClass method is available on the returned items, so you need a stop (.) not a comma (,)

The other gotcha is that you are writing the id in a foreach loop in PHP - I presume that each desk has a unique id - when you write jQuery("#<?php echo $desk ?>") the statement is outside of the foreach loop, so it won't match the ids you are targeting.

If you know that the desk is available in PHP, the best option would be to set the class as you write the desk...

<div id="<?php echo $desk?>" class="availableDesk"></div>

3 Comments

Also, he seems to be setting each div in the for loop to the same ID. HTML ID's need to be unique.
True - HTML ids should start with a letter and be unique. Based on the supplied code it is possible that they are, but there is also a chance they aren't.
In the HTML posted, there are 3 divs with the ID ddesk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.