I would like to use a jquery odometer to display information on a master page. http://www.jqueryscript.net/animation/Smooth-Animated-Numbers-with-Javascript-CSS3-odometer.html
In order to do that I have to retrieve that value from SQL Server using C#. Then I have to pass it to the jscript odometer in the html() as shown below. If I get the valuje - how do Isend it to the javascript?
<script>
setTimeout(function(){
$('.odometer').html('123222');
}, 1000);
</script>
Try this html
</head>
<body>
<script src="https://rawgit.com/HubSpot/odometer/v0.4.6/odometer.min.js"></script>
<style src="https://rawgit.com/HubSpot/odometer/master/themes/odometer-theme-default.css"></style>
<script>
odometerOptions = {
auto: false
};
</script>
<script type = "text/javascript">
$(function(){
var exampleOdometerValue = 123456;
var exampleOdometer = new Odometer({ el: $('.odometer-example')[0], theme: 'digital', value: exampleOdometerValue });
exampleOdometer.render()
setTimeout(function(){
exampleOdometerValue = exampleOdometerValue+100.07;
exampleOdometer.update(exampleOdometerValue);
}, 2000);
});
</script>
<div class="odometer odometer-example">123</div>
</body></html>
You can use the below implementation to pass the value from code behind to Jquery
This is one example how to do it.
First Declare a Public Variable in code behind
//Declare a Public Variable in code behind
public string odometervalue = "667";
Read the value in Jquery Like given below
<script>
$(function () {
setTimeout(function () {
//Get the value from serverside
var uid = '<%=odometervalue %>';
odometer.innerHTML = uid;
}, 1000);
});
</script>
Related
I have a code like this:
<html>
<head>
<title>Example formBuilder</title>
</head>
<body>
<div class="build-wrap"></div>
<div class="build-wrap"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="https://formbuilder.online/assets/js/form-builder.min.js"></script>
<script>
jQuery(function($) {
$(document.getElementsByClassName('build-wrap')).formBuilder();
});
</script>
</body>
</html>
If it was initialized by id, then I could have get data with something like this:
var fbEditor = document.getElementById('build-wrap');
var formBuilder = $(fbEditor).formBuilder();
document.getElementById('getJSON').addEventListener('click', function() {
alert(formBuilder.actions.getData('json'));
});
However, I am using classname to initialize form builder. Is there any way, when click on save, get the respective form-builder data? I am using https://formbuilder.online/
Here is jsfiddle: https://jsfiddle.net/xycvbj3r/3/
#PS: there could be numerous form builder inside php loop.
You can try this:
formBuilder.actions.getData('json');
Or:
formBuilder.actions.getData();
The live demo is here: http://jsfiddle.net/dreambold/q0tfp4yd/10/
I was facing the same issue too. This worked for me
var list = ['#ins1', '#ins2', '#ins3'];
var instances = [];
var init = function(i) {
if (i < list.length) {
var options = JSON.parse(JSON.stringify([]));
$(list[i]).formBuilder(options).promise.then(function(res){
console.log(res, i);
instances.push(res);
i++;
init(i);
});
} else {
return;
}
};
init(0);
And to get data, you can use instances[key].actions.getData()
I am not sure how you are planning to save this data, but to help with your problem of getting form data for a particular form you can use something like this
var formBuilder = $(document.getElementsByClassName('build-wrap')).first().data('formBuilder').actions.getData()
Or to use it over a jQuery Collection then
$(document.getElementsByClassName('build-wrap')).each(function () {
var formBuilder = $(this).data('formBuilder').actions.getData()
})
There is a callback mentioned in the documentation, onsave which runs on editor save. So, when clicking on any form builder's save button, the respected form's data can be received.
Here is the code-
<html>
<head>
<title>Example formBuilder</title>
</head>
<body>
<div class="build-wrap"></div>
<div class="build-wrap"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="https://formbuilder.online/assets/js/form-builder.min.js"></script>
<script>
jQuery(function($) {
var options = {
onSave: function(evt, formData) {
// This is the respected form's data
console.log('MY DATA_________', formData)
},
};
$(document.getElementsByClassName('build-wrap')).formBuilder(options);
});
</script>
</body>
</html>
Here is the fiddle (couldn't create a working snippet due to not working CDNs.
)- https://jsfiddle.net/nehasoni988/rpo1jnuk/1/#&togetherjs=Mka9TJ4cex
I have a set of KPI data I need to pass over to a Javascript file from my ASP.NET project. I thought I could do so using a ViewBag... Here is what is in the controller:
public ActionResult KPI()
{
if (Session["OrganizationID"] == null)
{
return RedirectToAction("Unauthorized", "Home");
}
else
{
int orgId;
int.TryParse(Session["OrganizationID"].ToString(), out orgId);
var user = db.Users.Find(User.Identity.GetUserId());
var organization = user.Organizations.Where(o => o.OrganizationID == orgId).FirstOrDefault();
var reports = db.Reports.ToList();
try
{
var org_reports = (from r in reports
where r.OrganizationID == organization.OrganizationID
select r).ToList();
var kpi = new KPI(org_reports);
var jsonKPI = JsonConvert.SerializeObject(kpi);
ViewBag.orgData = jsonKPI;
}
catch (ArgumentNullException e)
{
return RedirectToAction("Unauthorized", "Home");
}
}
return View();
}
From the View I've tried using hidden values, and also just passing them in as parameters when calling the script:
<input type="hidden" id="orgData" value=#ViewBag.orgData>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="~/Scripts/KPIs.js">
orgData = #ViewBag.orgData;
</script>
I then want to read this value in my JS script and parse it into JSON from the string:
function myFunction(){
var test1 = JSON.parse(document.getElementById('orgData'); // Doesn't work
var test2 = JSON.parse(orgData); // Doesn't work
}
It doesn't appear that any of these methods are working. What is my mistake here?
You should use Html.Raw, to avoid ASP.NET to escape your value:
orgData = #Html.Raw(ViewBag.orgData);
Also, if this is a Json, it is also a valid JS object, so you don't need to parse, it already is a JS Object.
It looks like you forgot the quotes.
<input type="hidden" id="orgData" value=#ViewBag.orgData>
should be
<input type="hidden" id="orgData" value="#ViewBag.orgData">
Also the code inside your script tag will never get executed because the script tag has a src attribute on it. Code inside script tags with src attributes never gets executed.
<script type="text/javascript" src="~/Scripts/KPIs.js">
orgData = #ViewBag.orgData;
</script>
should be changed to
<script type="text/javascript" src="~/Scripts/KPIs.js" />
<script>
orgData = #ViewBag.orgData;
</script>
I solved it! Pass the KPI model through the view and then it's as easy as:
var orgData = #Html.Raw(Json.Encode(Model));
Thanks to all to offered help.
i want to get the number in text field and send it in Url methode GET ;
$(document).ready(function () {
$("#go").click(function() {
var n = document.getElementById('nombre').value;
alert("Chess.php?number=9");
$('#chess').load("Chess.php?number="+n);
});
the value :
<input type="text" name="nombre" id="nombre">
Try again with code below:
$(document).ready(function () {
$('#go').click(function() {
$.get('Chess.php', { number: $('#nombre').val() } );
});
});
PS: Its stranger the attribute value called nombre and the field called number. Change this too. Other tip is dont use Uppercase for the url, use only 'chess.php'.
I hope to help you.
There's a missing }) ...
Plus you have to add in the head section of your script the following :
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function ()
{
$("#go").click(function()
{
var n = document.getElementById('nombre').value;
alert("Chess.php?number=9");
$('#chess').load("Chess.php?number="+n);
})
});
</script>
it works now like this
$(document).ready(function () {
$("#go").click(function() {
$('#chess').load("Chess.php?number="+document.getElementById('nombre').value);
});
});
I have a textbox where i must to write a value, how to pass this value in my controller if I call my controller using ActionLink, this is my code:
view:
#Html.TextBox("tisch", "", new { #class = "teschChange"})
#Html.ActionLink("Apply Command", "ApplyCommand", "Work")
and this is te script
<script type="text/javascript">
$(function test() {
$(".teschChange").change(function () {
var tisch = $(this).attr('value');
});
});
</script>
I must to send tisch in ApplyCommandController
Thanks
Instead of using an action link, use a standard link:
<a id="l" href="#Url.Action("ApplyCommand", "Work")">Apply Command</a>
Then, you can use this script:
<script type="text/javascript">
$(function test() {
$(".teschChange").change(function () {
var tisch = $(this).attr('value');
$("#l").attr("href", "#Url.Action("ApplyCommand", "Work")/" + tisch);
});
});
</script>
Which will assign a href of: /Work/ApplyCommand/tisch.
this was the correct script:
$("#l").attr("href", "#Url.Action("ApplyCommand", "Work")?tisch=" + tisch);
<div id="example">
</div>
<script type="text/javascript">
function insert() {
var data = '<script type="text/javascript"> function test() { a = 5; }<\/script>';
$("#example").append(data);
}
function get() {
var content = $("#example").html();
alert(content);
}
</script>
INSERT
GET
</body>
</html>
what i want to do:
when i click on insert, i want to insert this code into example div:
<script type="text/javascript"> function test() { a = 5; }<\/script>
when i click on get, i want to get that code, which i inserted there.
but when i click on insert, and then on get, there's no code.. where is problem ? thanks
According to your comment to Adam Bellaire, you want the script tag to display as normal text. What you are looking to do is encode the text with HTML entities, this will prevent the browser from processing it as normal HTML.
var enc = $('<div/>').text('<script type="text/javascript"> function test() { a = 5; }<\/script>').html();
$("#example").append(enc);
This works:
function insert() {
var data = '<script type="text/javascript"> function test() { a = 5; }<\/script>';
$('#example').text(data);
}
function get() {
var content = $('#example').text();
alert(content);
}
Are you checking for the code by browsing the live version of the DOM, using a tool like Firebug? If you are expecting to see your code rendered in your regular browser window, you won't, because the script tags are actually parsed when they are inserted, and script tags aren't visible elements in an HTML page.