I want to create some var in my JADE view in Node.js application, for example:
- var MyVariable = ''
And when i click on the button:
button(onclick="show('#{item._id}')")
I want to change MyVariable to value of item._id
function show(partner_id){
MyVariable = partner_id
}
Unfortunately, the code above won't work
Where are you passing item to the jade template? You should be able to simply assign MyVariable in the same place.
Related
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');
This is my javascript function :
function change(text){
var value = text;
}
This is my HTML code:
More
Now the above code works until I add "safe" filter to my django variable. When I'm calling the change function like this change('{{itm.description|safe}}') it doesn't works.
I have the following function:
<script>
function assign()
{
var val = "";
val = document.form1.text1.value;
alert(val);
}
I want to access the variable val's value inside jsp tag so that i can pass it to the googlePlus method as a String. I tried making var val as a global variable, but it doesnt work. How can I access the variable val inside the following code?
<%
String output = "";
if ( Boolean.valueOf(request.getParameter("submitted")) == true ) {
Scraping h = new Scraping();
output = h.googlePlus();
}
%>
You can assign the value of the variable using a assignment operator to some hidden JSP field and then use it in the JS using document.getElementById(). the code would be somewhat like:
<input type="hidden" value="<%=output%>">
Or alternatively if your js is residing inside the JSP only
var s = "<%=output%>"; should work!
Cheers!
You can't access javascript variables via JSP Java Code.
Your JSP & Java codes are compiled at server side.
And your javascript runs in a browser.
So send the 'val' variable to your servlet by form submit, ajax or whatever.
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.
I am using C# asp.net mvc3.
In one of my views, say I declare a variable like this:
#{
ViewBag.Title = "title"
string mystr;
}
I need to set the value of the variable mystr in a function of the script. How do I access this variable from the script?
Suppose I have a function like
<Script type="text/javascript">
function(){
var st = "This string is for the global variable"
mystr = st;
}
</script>
mystr will later be used in the html code like this:
<h2>#mystr</h2> .
Is there a similar way of accessing a variable from a function?
mystr in your example is server side variable that lives during cshtml view parsing. So you can't affect it with JS code on the client side. You can try something like this:
<script type="text/javascript">
window.mystr = #(mystr); // Set value while server side processing.
function(){
var st = "This string is for the global variable"
window.mystr = st; // Get/Set value
}
</script>
And use global variable mystr on client side wherever you need.
If you need then set header's text via JS you can do it for example with jQuery:
<h2 id="header"></h2>
JS:
$('#header').text(window.mystr);