I'm trying to use javascript variables inside laravel blade.
But I'm getting this error.
What should I do for using this ?
var activeTab = $("ul#tablist li.active");
var floor = {!! \App\Floor::find(activeTab.attr('id')) !!}
Use of undefined constant activeTab - assumed 'activeTab'
Ok, so you can have another route like this:
Route::post('/getFloorById', [
'uses' => 'YourController#getFloorById',
'as' => 'getFloorById'
]);
Then, in you controller you have the method:
private function getFloorById($segments)
{
$floorId = $segments[1];
return \App\Floor::find($floorId);
}
And last, you need your javascript code:
var floor = $.post('/getFloorById', {"id": activeTab.attr('id')});
This is the ajax method. Keep in mind that you might need to use echo instead of a return. And you might need to wrap your code into json_encode(\App\Floor::find($floorId)) like this, but for the most part this should get the job done.
You might want to call the post from an onclick event that way you get the new object every time the tab changes.
Unfortunately blade is just a framework to output php. You don't need to do that, and you won't be able to anyways. That would be a big security issue.
If you really need that floor use what #Rohan suggested, javascript directly:
var floor = activeTab.attr('id');
I have following long type session on server side code
long[] grouparray = ..;
Session["grouplist"] = grouparray;
Now I'm trying to get this session on View Page's jquery click function
$("#gpline").click(function () {
parseInt(#Session["grouplist"]);
var grouplistvalues = Session["grouplist"];
alert(grouplistvalues);
});
But this is having error once I debug using firebug
SyntaxError: expected expression, got ']'
parseInt(System.Int64[]);
You need to use Json.Encode and #Html.Raw on your c# data to make it compatible with your scripts.
Try this.
$("#gpline").click(function () {
var grouplistvalues = #Html.Raw(Json.Encode(Session["grouplist"])); // converting the session data into array of numbers in javascript variable
alert(JSON.stringify(grouplistvalues)); // stringify is used only to test.
});
i'm new on javascript and also working with json. i'm trying to get some json data and store some of them in keepGetData. but when I want to get an entry, it says TypeError.
what is wrong with my code?
TypeError: undefined is not an object (evaluating 'keepGetData[j] =
result[i]')
thanks in advance
var keepGetData;
function getList(){
$.getJSON(url, function(result){
var j=0;
for(var i=0,len=result.length;i<len;i++){
if(result[i].parentId == 1 && result[i].restaurantId == restId.id[0]
{
keepGetData[j] = result[i];
j+=1;
}
}
});
You need to initialize your variable:
var keepGetData = [];
Please do use strict mode by writing "use strict" in the first line of your Javascript files. That catches the use of uninitialized variables much easier.
var keepGetData=[];
You should use above code.You need to define the variable as array.
I've been at this for an hour and I need help. This is kind of baffling me. Consider this explicit setup of an object in my code:
WORKING CASE:
var terms={};
terms[0]={};
terms[1]={"label":"bag","cell_src":"images/bag.jpg","clue_type":"audio","clue_src":"/audio/bus.wav"};
terms[2]={"label":"crayon","cell_src":"images/crayon.jpg","clue_type":"audio","clue_src":"/audio/car.wav"};
terms[3]={"label":"pen","cell_src":"images/pen.jpg","clue_type":"audio","clue_src":"/audio/car.wav"};
terms[4]={"label":"pencil","cell_src":"images/pencil.jpg","clue_src":"/audio/boat.wav"};
terms[5]={"label":"pencil_case","cell_src":"images/pencil_case.jpg","clue_src":"/audio/train.wav"};
terms[6]={"label":"rubber","cell_src":"images/rubber.jpg","clue_src":"/audio/taxi.wav"};
terms[7]={"label":"ruler","cell_src":"images/ruler.jpg","clue_src":"/audio/plane.wav"};
terms[8]={"label":"sharpener","cell_src":"images/sharpener.jpg","clue_src":"/audio/taxi.wav"};
window.terms= terms;
window.terms= terms; // for using globaly
if I do a console.log(window.terms[1]); I get "bag". Thats what I want.
NOT WORKING CASE
If instead of explicitly defining the values of term{}, I read in the contents from a json file and assign them to each enumerated index like this:
var terms={};
terms[0]={};
$.getJSON('content.json', function(data){
$.each(data,function(i){
//terms[i]={"label":"bag","cell_src":"images/bag.jpg","clue_type":"audio","clue_src":"/audio/bus.wav"};
terms[i+1]={"label":data[i].headword,"cell_src":data[i].image,"clue_type":"audio","clue_src":data[i].audio};
});
window.terms=terms;
});
if I do a console.log(window.terms[1]); I get an error "Uncaught TypeError: Cannot read property '1' of undefined" Note that I have an alternate attempt commented out where I eliminate the possibility that theres something weird going on with the values I am trying to pull in and I explicitly assign the same static value to all the indexes. That produces the same error.
Any ideas how this could be??
$.getJSON does not block when performing an AJAX call. You have to keep the callback chain a live.
I think you want to define terms as an array of objects. Currently you have it defined as an object with properties 1, 2, 3, etc. Syntax like var terms = {} means terms is an object and when you assign terms[1] = {"label": "bag"} you're saying "the property named 1 of object terms is {"label": "bag"}. Just change your terms declaration to this:
var terms = [];
Also, if you want to see the label property of one of the objects the log statement would looks like this:
console.log(terms[2].label);
The $.getJSON() function is just a shorthand for a call to $.ajax() to load a JSON file. Since the AJAX call is asynchronous, the execution of $.getJSON() completes, and any code after it is executed, before the data has been loaded and stored in your variable.
If you want to work with terms do so inside the success callback function that you're passing to $.getJSON().
If your code looks like this:
var terms={};
terms[0]={};
$.getJSON('content.json', function(data){
$.each(data,function(i){
//terms[i]={"label":"bag","cell_src":"images/bag.jpg","clue_type":"audio","clue_src":"/audio/bus.wav"};
terms[i+1]={"label":data[i].headword,"cell_src":data[i].image,"clue_type":"audio","clue_src":data[i].audio};
});
window.terms=terms;
});
// use window.terms here
Then it won't work, because the // use window.terms here part executes before the AJAX call has finished. You'll need to move that to a separate function and call that from the success callback:
function workWithTerms() {
// use window.terms here
}
var terms={};
terms[0]={};
$.getJSON('content.json', function(data){
$.each(data,function(i){
//terms[i]={"label":"bag","cell_src":"images/bag.jpg","clue_type":"audio","clue_src":"/audio/bus.wav"};
terms[i+1]={"label":data[i].headword,"cell_src":data[i].image,"clue_type":"audio","clue_src":data[i].audio};
});
window.terms=terms;
workWithTerms();
});
I am mixing Javascript and Razor code to redirect to a page sending a couple params.Heres my code that I tried.
function insertSuccess(data) {
$('#NewCategoryGroup').dialog('close');
var myId = data.Object.GroupCatId;
var myName = data.Object.ItemCategoryGroup;
window.location.replace("#Url.Action("CatGroupDetails","TaxRules",new { id = myId, name = myName })");
}
I can replace Id = 1 and name = "test" and it works. But when I try to use variables it wont let me. Do I have to write my URL the old way -- "http://www.someweb.com/5?Name=test".
Thanks for the help.
I think in your code above myId and myName are javascript variables, but you are trying to use them under the C-Sharp scope. Url.Action is a C# function here.