1

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?

3
  • make sure that the controller is passing the variable to the view that includes this script Commented Jun 21, 2018 at 18:47
  • yes that blade view getting variable from controller ? Commented Jun 21, 2018 at 18:51
  • getting this : w.fn.init [div#question_id] in console when is print console.log(id); Commented Jun 21, 2018 at 18:52

2 Answers 2

1

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; ?>);
Sign up to request clarification or add additional context in comments.

2 Comments

what data-field-id means here?
how i can get my previous page id value ? in javascript variable
0

You can use in JS

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

1 Comment

it shows undefined until i put that into foreach loop like @foreach($getquestions as $value) var id = $("#question_id").html ('{{ $value->question_id }}'); @endforeach

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.