I'm trying to assign a javascript variable data from ror. I already made the query, and it gives me what i want (a single integer), but i can't assign it to a js variable. Here is the js script i'm using:
<script type="text/javascript">
$(function() {
var number_of_products = '<%= Post.where(:id => 1).select(:amount).pluck(:amount)[0] %>';
$(document).ready(function(){
for (i=1; i <=number_of_products; i++) $('#itemsAmount').append('<img src="images/box.svg"/>');
})
})
</script>
But i can't seem to pass the ror value (<%= Post.where(:id => 1).select(:amount).pluck(:amount)[0] %>) to the js variable (number_of_products)
Any ideas?
Thanks,
Shai.
It would seem your problem isn't with getting the value into javascript, its simply the wrong type try and change your code to the following
<script type="text/javascript">
$(function() {
var number_of_products = <%= Post.where(:id => 1).select(:amount).pluck(:amount)[0] %>;
$(document).ready(function(){
for (i=1; i <=number_of_products; i++) $('#itemsAmount').append('<img src="images/box.svg"/>');
})
})
</script>
Edit:
Do you have #itemsAmount on your page?
Working fiddle http://jsfiddle.net/DeMd2/
Related
How to use var aid value assign into $data array? I am getting error: "Use of undefined constant aid". I am working on Laravel 5.4. I want to show ($data['services'][aid]->servicesubdetails) value in tage. My code is below:
<script language="JavaScript">
function theFunction(e)
{
var aid= e.target.id;
$("p").html('{{ ($data['services'][aid]->servicesubdetails) }}');
}
</script>
Single quotes are interrupted by another single quote.
<script language="JavaScript">
function theFunction(e)
{
var aid= e.target.id;
$("p").html(“{{ ($data['services'][aid]->servicesubdetails) }}”);
}
</script>
Notice “ instead of ‘.
EDIT
You can use json to use you PHP array in javascript
Look at LaravelDisplayData and PHPJsonEncode
<script language="JavaScript">
var data = #json($data)
function theFunction(e)
{
var aid= e.target.id;
$("p").html(data.services[aid].servicesubdetails);
}
</script>
I want to pass the value i got it from model to the java-script function
<script type="text/javascript">
var checkin = #Model.Parameters.Checkin.ToString("dd-MM-yyyy");
var checkout = #Model.Parameters.Checkout.ToString("dd-MM-yyyy");
</script>
this function that i want to pass model chick-in and chick-out value to it:
$('document').ready(function () {
$("#Arrival").val(checkin);
$("#Departure").val(checkout);
});
i tried many solution but it didn't work yet .
any advice, thanks
if the #Model.Parameters.Checkin and #Model.Parameters.Checkout not null then Try:
<script type="text/javascript">
$( document ).ready(function(){
var checkin = '#Model.Parameters.Checkin.ToString("dd-MM-yyyy")';
var checkout = '#Model.Parameters.Checkout.ToString("dd-MM-yyyy")';
$("#Arrival").val(checkin);
$("#Departure").val(checkout);
});
Just you miss '. and also change $('document').ready(function () { }) to $(document).ready(function () { }).
you must write all script into a .cshtml file. #Model.Parameters.Checkin.ToString("dd-MM-yyyy") never work into a .js file.
Because, In .cshtml, when the the page is render then it white to HTTP response stream as a string.
In MVC, you can use following code:
<script type="text/javascript">
var number = parseInt(#ViewBag.Number); //Accessing the number from the ViewBag
alert("Number is: " + number);
var model = #Html.Raw(#ViewBag.FooObj); //Accessing the Json Object from ViewBag
alert("Text is: " + model.Text);
</script>
I have a JavaScript function that I wrote and embedded in an .aspx page. I declared variables at the top of the function that access ConfigurationManager.AppSettings and also the sessionState tag as follows:
var Value1 = "<%= System.Configuration.ConfigurationManager.AppSettings["Value1"].ToString()%>";
var Value2 = "<%= Session.Timeout %>";
This function has been working just fine. I've realized that I will need to use the function across four other pages so I decided to move it to an external JavaScript file. According to this question and accepted answer...
Accessing ConfigurationManager.AppSettings in Java script
...external JavaScript files do not evaluate code inside of these "server-side code" tags, therefore, values from the web.config file must be passed from the .aspx page as parameters. I moved the function to an external JavaScript file and call the function like this:
<script src="Scripts/JavaScript.js" type="text/javascript">
var Value1 = "<%= System.Configuration.ConfigurationManager.AppSettings["Value1"].ToString()%>";
var Value2 = "<%= Session.Timeout %>";
externalFunction(Value1, Value2)
</script>
The external JavaScript functions begins as follows:
function externalFunction(Value1_, Value2_) {
debugger;
var Value1 = Value_1;
var Value2 = Value_2;
...
}
As I debug the JavaScript function, the arguments themselves are undefined. What am I missing here?
Update - 4/12/17
I have tried calling the function in both of the following ways.
<script src="Scripts/JavaScript.js" type="text/javascript">
var Value1 = "1";
var Value2 = "2";
externalFunction(Value1, Value2);
</script>
<script src="Scripts/JavaScript.js" type="text/javascript">
var Value1 = "1";
var Value2 = "2";
</script>
<script src="Scripts/JavaScript.js" type="text/javascript">
externalFunction(Value1, Value2);
</script>
Using IE's debugger, I can see that the values were pulled correctly from the web.config file, but the function still is not being called. I'm stumped.
Try with two separate script tags, and see any different.
<script src="Scripts/JavaScript.js"></script>
<script type="text/javascript">
var Value1 = "<%= System.Configuration.ConfigurationManager.AppSettings["Value1"].ToString()%>";
var Value2 = "<%= Session.Timeout %>";
externalFunction(Value1, Value2);
</script>
I just need your help about my code. My problem is how can I access smarty variable within jquery or javascript file? Becuase my smarty variable is a URL request from my controller. And I need to use that variable for creating validation. Here's my code.
{$get.search_by} {**works without error**}
{literal}
<script type="text/javascript">
$(document).ready(function(){
var dispatch = "{$get.search_by}"; //can't access
var new_class = "it3 ir3 il3 jt10 jr05 jl05 kt03 kr04 kl04";
var old_class = "it3 ib3 il3 jt05 jb05 jl10 kt04 kb04 kl03";
var toggleState = true;
//could not access
if(dispatch == companies.catalog){
alert("catalog");
}else{
alert("product search");
}
console.log(dispatch);
Try this code
var dispatch = '{/literal}{$get.search_by}{literal}'
To make things cleaner, you can move the {literal} tag down and also escape the $get.search_by variable (in case search_by may have a string with a quote i.e. "let's try"):
<script type="text/javascript">
var dispatch = '{$get.search_by|escape:'javascript'}';
{literal}
I am using will_paginate in order to list huge number of files. I also have a check_box in order to choose files for the futher analysis.
To save the ids in the cookie while changing the pages I used following javascript:
<script src="/assets/jquery.cookie.js" type="text/javascript"></script>
<script type="text/javascript">
var checkedIds = []
$('.checkmeplease').on("change", function(){
if($(this).is(':checked')) {
checkedIds.push($(this).val())
}
else {
checkedIds.splice(checkedIds.indexOf($(this).val()), 1);
}
$.cookie('checked_file_ids', checkedIds,{path:'/'});
});
</script>
My checkboxes:
<% #files.each do |file| %>
<p><td> <%= check_box_tag "files[]", file.id,false,:class=>'checkmeplease' %></td> <%= file.name %></p>
<%end%>
It saves the IDs but when I change the page with will_pagination, the saved IDs disappear.
I was introduced as well this code:
<script type="text/javascript">
var checkedIds = $.cookie('checked_file_ids');
$('p td input[type=checkbox]').on('change', function () {
if($(this).is(':checked')) {
checkedIds.push($(this).val())
}
else {
checkedIds.splice(checkedIds.indexOf($(this).val()), 1);
}
$.cookie('checked_file_ids', checkedIds);
})
</script>
But $('p td input[type=checkbox]').on('change', function () does not seem to work.
Where is my mistake? How can I save the IDs from the previous pages as well?
Thanks a lot.
Use your first example, but instead of this line:
var checkedIds = []
Use this line from your second example.
var checkedIds = $.cookie('checked_file_ids');
The problem I see with the first example is that you never load the previously saved ids from the cookie. So, come page two, when you check some, you're overwriting the previous values.