mmoore
asked on
How to use jQuery to reference a javascript variable
I've installed the jQuery currency formatter plugin found here. https://code.google.com/p/jquery-formatcurrency/
The value I want formatted is in a javascript variable. This is an excerpt from a jTable script:
The comment is just something I tried but did not work. I've tried dozens of others too, but I can't get this to work.
I know I am getting into the formatCurrency function because Chrome's Developer Tools shows me the function source code.
TIA for any help here.
The value I want formatted is in a javascript variable. This is an excerpt from a jTable script:
dollarAmount: {
title: 'Amount',
width: '25%',
display: function (data) {
//return $("#" + data.record.dollarAmount).formatCurrency();
return (data.record.dollarAmount).formatCurrency();
}
The comment is just something I tried but did not work. I've tried dozens of others too, but I can't get this to work.
I know I am getting into the formatCurrency function because Chrome's Developer Tools shows me the function source code.
TIA for any help here.
ASKER
Thanks Rob,
Here is the complete page to give you some context.
commissionrateadjustment-view.jsp.txt
Here is the complete page to give you some context.
commissionrateadjustment-view.jsp.txt
Honestly, it's no use to me as I can't test the data you're returning from "/commission/CRUDControlle r?action=l ist". I could use my own sample data but it could be your data that's the issue.
What output did you get from the console.dir() actions?
What output did you get from the console.dir() actions?
ASKER
Sorry I'm not giving you the pieces you need to solve the puzzle. "/commission/CRUDControlle r?action=l ist". returns a JSON document. This is the script's web site. : http://www.jtable.org/
I KNOW that at the moment of displaying, data.record.dollarAmount DOES contain the actual dollar amount that I want displayed. I know this because I did alert(data.record.dollarAm ount). The problem is passing that value to the formatCurtency() function and getting a formatted value back. I am getting either null or blank back. I'll try using the console.dir() as you suggested.
Thanks,
Mike
I KNOW that at the moment of displaying, data.record.dollarAmount DOES contain the actual dollar amount that I want displayed. I know this because I did alert(data.record.dollarAm
Thanks,
Mike
ah ok... thanks, just needed to confirm and rule that out. Would you mind posting a screenshot of the alert message? Unless you can tell me whether data.record.dollarAmount is already formatted in any way or is it just a float, e.g. 48522.88 or is it 48,522.88 etc. If there's anything but a number it could be what's bombing the formatCurrency() function. Sorry if that sounds trivial, I'm just trying to rule everything out.
The issue here is that the formatCurrency() needs to act on a DOM element, not a JSON object. You need to set it to an element and then call the function:
http://jsbin.com/huvuda/1/edit?html,js,output
http://jsbin.com/huvuda/1/edit?html,js,output
$(function() {
var data = {
record: {
dollarAmount: 474747728
}
}
$("#myid").text(data.record.dollarAmount).formatCurrency();
});
<div id='myid'></div>
ASKER
So there is no way to jQuery directly reference a javascript variable. Okay. I'll take a moment to make some changes per your link.
ASKER
I think I'm close:
<div id="Foo"></div>
larAmount. display (http://localhost:18080/commission/commissionRateLevelAdjustment.do:490:54)↵ at c.widget._getDisplayTextFo rRecordFie ld
--snip--
<div id="Foo"></div>
display: function (data) {
$( "#Foo" ).data("bar",data.record.dollarAmount);
//return $("#Foo").data("bar"); // this returnes the unformatted dollar amount
return $("#Foo").data("bar").formatCurrency(); //this results in TypeError
}
"TypeError: undefined is not a function↵ at Object.$.jtable.fields.dol--snip--
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Rob,
I actually came up with this after trying what seems to be a thousand variations:
Mike
I actually came up with this after trying what seems to be a thousand variations:
display: function (data) {
document.getElementById("Foo").textContent=data.record.dollarAmount;
$("#Foo").formatCurrency();
return document.getElementById("Foo").innerText;
}
Yours is a little more concise so I'll go with it. Thanks so much for your help! Mike
Glad you got something you can work with. Happy I could help.
Cheers,
Rob
Cheers,
Rob
You should add in console.dir() calls to see what the data object contains (it will show up in the console of the dev tools). What are the results of the below?
Open in new window