0

I want to write echo php variable in jquery without single or double quote. Below is my code

jQuery.noConflict();
jQuery(document).ready(function(){
jQuery('.your').slick({
 autoplay: <?php echo $autoPlay; ?>,
 autoplaySpeed: 2000,
 dots: true,
 infinite: true,
 speed: 300,
 slidesToShow: 4,
 slidesToScroll: 1,
 responsive: [
 {
  breakpoint: 1024,
  settings: {
    slidesToShow: 3,
    slidesToScroll: 3,
    infinite: true,
    dots: true
  }
},
{
  breakpoint: 600,
  settings: {
    slidesToShow: 2,
    slidesToScroll: 2
  }
},
{
  breakpoint: 480,
  settings: {
    slidesToShow: 1,
    slidesToScroll: 1
  }
},{
  breakpoint: 320,
  settings: {
    slidesToShow: 1,
    slidesToScroll: 1
  }
}
]
});
});

I want to echo $autoplay without double quote. When i write without double quote, its gave me error

Uncaught SyntaxError: Unexpected token ,

Any one help me to find out the exact solution for this

2
  • What does $autoplay look like? Is it a string like true or false? Commented Jan 7, 2015 at 17:06
  • yes $autoplay = true or false Commented Jan 7, 2015 at 17:07

1 Answer 1

2

Perhaps there's no value to $autoPlay.. you can prevent errors such as that by providing a default value... example:

autoplay: <?php echo isset($autoPlay) ? $autoPlay : '0'; ?>,

If autoplay is a boolean, then you need to echo something else, such as:

autoplay: <?php echo $autoPlay ? '1' : '0'; ?>,

Both of these use ternary operators - see http://php.net/ternary#language.operators.comparison.ternary

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

5 Comments

If autoplay is a boolean, try my second example.
I used your second example but its doesn't run
then autoplay: <?php echo $autoPlay ? 'true' : 'false'; ?>, would work. Either way "1" and "0" (without quotes) in JS is evaluated as true/false... so I don't see the issue. Maybe issue lies elsewhere. Are you sure $autoPlay has something in it? Try using var_dump($autoPlay) to make sure.
I used ternary operation and resolved (Mage::getStoreConfig('productslider/new_viewed_config/new_autoStart_block_enable') == 1) ? 'true' : 'false';
Sometimes it helps to know what variables you are using, in what system, etc.. we usually try to answer in an agnostic fashion. Be sure to mark as answered!

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.