-2

I found this question to be exactly the same problem I'm facing, but the answers suggested using objects for dynamic access. This might be well and good, but I'm looking for the exact simple answer on how to include a variable in a function's name.

I'm using ClockPick and dare not mess up the code, so I can't use objects or anything else. The problem is that with [ $("#clockpick"+x).clockpick ] the result isn't [ $("#clockpick0").clockpick ] but instead [ $("#clockpick"+x).clockpick ].

This all happens inside a PHP loop and it looks something like this:

var x = 0; (declared previously outside of the loop)
<script>
function doit()
{
  $("#clockpick"+x).clockpick
  ({
      starthour: 7,
      endhour: 20
      ...
  });
}
x++;

timepicker.php

<script>var times = 0;</script>
<?php
$goo = $_POST['goo'];
for ($foo = 0; $foo < $goo; $foo++)
{
  ?>
  <script>
  function clocker()
  {
  $("#clockpick"+times).clockpick
  ({
    starthour: 7,
    endhour: 20
  });
  } times++;
  </script>
  <?php
  print "<input type='text' id='clockpick$foo' onclick='clocker()' />
?>

As mentioned, this works ok if I manually set "times" to a number, but as you can see, I don't know what number $goo has. In all, this is still a simplified demo from the actual page of 153 rows.

7
  • Can't see PHP or any other loop here. Commented Oct 25, 2013 at 8:11
  • Seems fine. How do you know the result is wrong? Commented Oct 25, 2013 at 8:15
  • And this has nothing to do with the linked question, here your jquery selector is variable (which should always work), not your function name. Commented Oct 25, 2013 at 8:17
  • Is x a PHP variable or a Javascript variable? Can you show us your PHP as well? Commented Oct 25, 2013 at 8:28
  • Artyom - I didn't include the whole page in the example. Basically php loops an input field that's tied to this js so that when a user clicks on the field, it should trigger this script. It works all right if I manually test it like this [ $("#clockpick5").clockpick ]. It then works on the fifth textfield, but since I don't know how many fields are going to be generated, I thought of using js variable mentioned previously. So I know the code works just fine, but the problem is that I can't figure out how to get that "x" variable to the function name. Commented Oct 25, 2013 at 8:31

1 Answer 1

0

You are using a javascript variable to be incremented in a php loop. The problem is that php will just write your javascript code as is, printing $("#clockpick"+times) at each iteration of the php loop. To achieve what you want to do, you should use $foo instead of the useless javascript variable times like this

<?php
$goo = $_POST['goo'];
for ($foo = 0; $foo < $goo; $foo++)
{
?>
<script>
function clocker()
{
<?php
print "$('#clockpick$foo').clockpick"
?>
({
    starthour: 7,
    endhour: 20
});
}
</script>
<?php
print "<input type='text' id='clockpick$foo' onclick='clocker()' />"
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, now it works just like a dream. Though it still doesn't call the script, but I guess that's another problem altogether.

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.