3

Can I pass blade variable to javascript code:

var deadline = 'Octobar 20 2015 22:00:00 UTC+0200';
initializeClock('clockdiv', deadline);  
    };

How can I pass deadlinevariable with {{$article->auction_end}} ? Is that possible or do I need to make an ajax call ?

1 Answer 1

6

What I usually like to do because I think it's cleaner than directly setting php variables as javascript variables is to generate a meta tag for it instead.

<meta name="deadline" content="{{ $article->auction_end }}" >

Then you can grab it later with jquery.

var deadline = $('meta[name=deadline]').attr('content');

This way is especially helpful if you want to break out the js into its own file rather than have a bunch of javascript in your views.

However, yes it is possible to set your javascript variable with PHP, assuming your javascript is inside your .blade.php file...

var deadline = '{{ $article->auction_end }}';
Sign up to request clarification or add additional context in comments.

1 Comment

nice trick! btw, for HTML5 ` />` is not nessesary: stackoverflow.com/a/3558200/1331425.

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.