prasoon mudgal
prasoon mudgal

Reputation: 31

Laravel : how to get database value in my java script variable?

i am new laravel ...facing problem in getting value in javscript variable below code :

 var id = $("#question_id").html('{{ $value->question_id }}');

Using div in blade part

<div id="question_id">{{ $value->question_id }}</div>

how to get question id in var id?Getting error value not define i use foreach loop it shows nothing? how to proceed?

Upvotes: 1

Views: 1067

Answers (2)

Zualex
Zualex

Reputation: 54

Maybe you can use data attributes to pass variables

<div id="question_id" data-field-id="{{ $value->question_id }}"></div>

<script>
    var id = $("#question_id").data("field-id");
</script>

It is still possible that some of this will work:

var id = $("#question_id").html("{{ $value->question_id }}");
var id = $("#question_id").html("{!! $value->question_id !!}");
var id = $("#question_id").html(<?php echo $value->question_id; ?>);

Upvotes: 1

Sunil kumawat
Sunil kumawat

Reputation: 804

You can use in JS

var id = $("#question_id").text();

Upvotes: 0

Related Questions