1

I am pulling my id's from a database with a foreach loop in a html file using

<div id="<?php print ($schedule['boxA']); ?>">

The question is how to use id's generated this way in a jQuery script?

 <script> 
 jQuery('I NEED TO INPUT ID HERE').datetimepicker({
     datepicker:false,
     format:'H:i',
     step: 15,
 });

</script>
1
  • $('#IDOFDIV') Replace IDOFDIV with the ID of the div.. Commented Jan 20, 2015 at 13:30

3 Answers 3

1

I would (and do) use a different pattern. You don't need to use the ID to reference an element with jQuery, so you can add a datetimepicker widget more generically by using the class selector.

HTML (inside foreach loop):

<div class="timepicker" id="<?php echo $schedule['boxA']; ?>">

JavaScript (only once):

<script> 
jQuery('.timepicker').datetimepicker({
    datepicker:false,
    format:'H:i',
    step: 15,
});
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

You would use the generated id's the same way that you always do in jQuery.

jQuery('#generated_id') // replace 'generated_id' with the id supplied by PHP

If you are wanting to echo the id's you could do something like this when the page is being rendered -

jQuery("#<?php echo $schedule['boxA']; ?>").datetimepicker({

7 Comments

<script> jQuery('#<?php print ($schedule['boxA']); ?>').datetimepicker({ datepicker:false, format:'H:i', step: 15, }); </script> Puting php directly in to the script is not working
Are you trying to echo jQuery to your page?
i am trying to generate jquery time pickers for various inputs and the inputs id's are stored in a database
So you're using those database values to output a page complete with jQuery, right?
Is there a specific problem with the output? Doesn't echoing the id work?
|
0

Bad idea.

I would suggest something like:

<div id="boxA" data-id="<?=$schedule['boxA']?>">

Or similar. The ID should be constant and known - that's what makes it an IDentifier. data-id can then be used to hold the server's ID reference to the thing in question.

2 Comments

if I use data id, what is the way to input the data-id in the jquery script? should i alter the script?
$("#boxA").data("id") will retrieve the ID.

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.