Reputation: 87
I have created this script to explode numbers from one data-get via php:
var n_polls=<?php echo $t_random_number;?>;
var myArray=n_polls.split(','); //explode
for (i=0;i<4;i++)
{
$("#t_sl_poll_"+myArray[i).hide();
}
The idea is to give some numbers from php for a random poll system, and I want explode this for close all in loop by the id. The problem is, I see something fail into the explode function for javascript, all time giving me nothing. How can I fix this?
Thank you.
Upvotes: 1
Views: 163
Reputation: 24655
your code should work as
var myArray= [<?php echo $t_random_number;?>];
for (i=0;i<4;i++)
{
$("#t_sl_poll_"+myArray[i]).hide();
}
Upvotes: 0
Reputation: 360782
Why explode in Javascript when you could have PHP just insert an array?
<?php
$numbers = array(1,2,3,4);
?>
<script type="text/javascript">
var n_polls = <?php echo json_encode($numbers); ?>;
for (i in n_polls) {
$("#t_sl_poll_" + n_polls[i]).hide();
}
There's further optimizations that could be done, but this'd be one place to start.
Upvotes: 2
Reputation: 57184
There are two things wrong here.
First: since you apparently hand over a string and not a number from php to javascript you have to write: var n_polls="<?php echo $t_random_number;?>";
Second: as pointed out in the comments already, you have to write $("#t_sl_poll_"+myArray[i]).hide();
to acutally address and element of your array.
Upvotes: 0
Reputation: 39540
Take a look at your source. Your error console would tell you the same thing. You're not putting your array (not a number) in quotes, so it fails to compile during runtime:
var n_polls="<?php echo $t_random_number;?>";
//QUOTES! ^ ^
var myArray=n_polls.split(','); //explode
for (i=0;i<4;i++)
{
$("#t_sl_poll_"+myArray[i]).hide(); //Missing bracket
// ^
}
Now it'd compile to
var n_polls="1, 2, 3, 4";
instead of
var n_polls=1, 2, 3, 4; //Useless non-working code - not enclosed in anything
Upvotes: 0