0

how can I access javascript variables in blade laravel?

for example

  var counter = 1;


 {{ dd(counter) }}

it gives me this error

Use of undefined constant counter - assumed 'counter' (this will throw an Error in a future version of PHP)

1
  • 1
    PHP runs server-sider, JavaScript runs in the browser. No. Commented Aug 7, 2020 at 1:54

2 Answers 2

1

You can't. You need to use JavaScript to inject the values to the Blade templates. Basically you need to do this in a script tag.

<p id="counter"></p>

<script>
var counter = 0;

counter++;

document.querySelector('#counter').innerHTML = counter;
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

you know I have a dynamic input that when user click on + new input is come . so I want to use this "counter" for old() helper laravel
In that case, you need to create the inputs dynamically and add them to the container element using the appendChild function (w3schools.com/jsref/met_node_appendchild.asp).
I am using append .. but each new inputs are a group that have a old()
0

Its better to add any id where you want to show veriable then select this element using query selector

<div id="show_variable"></div>

and use jquery

$(document).ready(function(){
 var countor = 0;
 counter++;
 $('#show_variable').html(counter)
});

3 Comments

you know I have a dynamic input that when user click on + new input is come . so I want to use this "counter" for old() helper laravel
its mean you are using counter as a id to identify input ?
something like this

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.