Reference a server control using Javascript/Jquery - javascript

I'm passing a server control name to a javascript function.
function SelectAllCheckboxes(chk, ctrlName) {
alert(ctrlName);
The alert returns "gridPublishers"...
So...
Why does this work:
$("#MainContent_GridPublishers_gridPublishers").hide();
But this doesn't :
$("#MainContent_GridPublishers_" + ctrlName).hide();
also...
alert($("#MainContent_GridPublishers_" + ctrlName).length);
returns "0"
alert('T' + ctrlName + "T");
returns "TgridPublishersT"
console.log("#MainContent_GridPublishers_" + ctrlName);
returns "#MainContent_GridPublishers_GridPublishers "

Related

Prevent Javascript from replacing beginning of string with NaN?

I am work on some OData query calls to Microsoft CRM and need my query in a very specific format.I am passing parameters to a function which then adds the URL to my query. What I am passing my retrieve function is as follows:
webAPI.REST.retrieveEntity(
"EntityDefinition",
id,
+ "/Attributes(LogicalName='" + logicalAttribute + "')"
+ "/Microsoft.Dynamics.CRM.PicklistAttributeMetadata"
+ "?$select=LogicalName&$expand=OptionSet"
, null)
In debugging my parameter with the query options is:
"NaNmylogicalattribute')/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet"
As you can see, my "/Attributes(LogicalName=" was replaced with "NaN". How do I prevent this from occurring?
Here is the problem:
id,
+ "/Attributes(LogicalName='" + logicalAttribute + "')"
Since you aren't starting with a String, the leading + is coercing your String into a Number (it isn't a Number, hence NaN). Just remove that first leading + and it will work:
id,
"/Attributes(LogicalName='" + logicalAttribute + "')"

how can call javascript from c# ext.net

in my project when i run it in chrome it is showing wrong time. explorer it is showing true. So i write this js code but it is still doesnt working.
This is my js;
var FormatTrxStartDate = function (value, record) {
var processDate = new Date(value);
return processDate.getDate() + "." + (processDate.getMonth() + 1) + "." +
processDate.getFullYear() + " " + processDate.getHours() + ":" +
processDate.getMinutes() + ":" + processDate.getSeconds();
};
And this is where i use it;
<ext:ModelField Name="TrxStartDate" Type="String" >
<Convert Fn='FormatTrxStartDate' />
</ext:ModelField>
Pass Parameter Value in not proper Date Format
Use processDate.getFullYear()==> processDate.getFullYear().toDateString() date data convert into String
X.Js.Call("FormatTrxStartDate ",value,record);
please try this code

Pass multiple parameters in view when using a string to call a JS function in MVC3

I am calling a function with one parameter "modalXPTO.HtmlFieldPrefix" sucessfuly like this:
modalXPTO.AdditionalJavaScriptCallback = "myFunction('" + modalXPTO.HtmlFieldPrefix + "')";
However, I would like to send more than one parameter, and I would expect this should work:
modalXPTO.AdditionalJavaScriptCallback = "myFunction('" + "{parentContainer:" + modalXPTO.ModalDivId + ", htmlFieldPrefix:" + modalXPTO.HtmlFieldPrefix + "}" + "')";
But it doesn't. Can anyone tell me what I am missing?
Here is the myFuntion declaration:
function myFunction(arg){
alert(arg.htmlFieldPrefix);
alert(arg.parentContainer);
}
I've fixed it.
modalXPTO.AdditionalJavaScriptCallback = "myFunction(" + "{parentContainer:'" + modalXPTO.ModalDivId + "', htmlFieldPrefix:'" + modalXPTO.HtmlFieldPrefix + "'}" + ")";
It is because of the misplaced "'". I wanted to pass an object with two parameters, but because of these characters I was passing a string instead.

Javascript - accessing single name value pair not working

Can anyone tell me why this is not working?
var fieldsValid = {
registerUserName: false,
registerEmail: false,
registerPassword: false,
registerConfirmPassword: false
};
function showState () {
var str = "<p>registerUserName: " + fieldsValid[registerEmail] + "</p>" ;
document.getElementById('showstate').innerHTML = str;
}
showState();
There is no output into my div.
Use quotes around the property name because otherwise, registerEmail is treated as a variable containing the property name, not a property name:
var str = "<p>registerUserName: " + fieldsValid['registerEmail'] + "</p>" ;
Or use the . syntax without quotes:
var str = "<p>registerUserName: " + fieldsValid.registerEmail + "</p>" ;
MDN Working With Objects is a good resource, relevant to this.
For future debugging, observe the console (F12) in your browser.
Let's say you have someObject.
someObject[johndoe] Returns the item in someObject that has johndoe's value (since here it is a variable) as index.

Parsing Complex JSON Javascript

I am able to parse JSON that returns simple data, with JSON.parse but I am having trouble with data that returns objects, dates, strings, etc..
var theData=JSON.parse(theData);
Something like this JSON.parse returns [Object] object back with no data at all (I can see the data is being successfully returned because it returns all the data as a string if I have JSON.parse turned off).
{
"AppName": "TheName",
"AppUrl": "https:\/\/app\/icons\/unknown.png",
"aGUID": "45c055d2-2edc-d4444"."DateCreated": "8\/23\/2012 11:04AM", {
"ID": "yser123",
Name ":" User "}
}
What is the best way to go about parsing this data in javascript(I am not able to use jquery)?
Note: I had wrote the JSON assume its valid
Here is the code I am using to retreive the data..
var xhReq = new XMLHttpRequest();
xhReq.open("POST", "ClientService.svc/REST/GetDetail", false);
xhReq.send(null);
var serverResponse = xhReq.responseText;
alert(serverResponse);
return serverResponse;
First and foremost, don't use synchronous XHR. Rewrite your JavaScript to be asynchronous.
function getDetail(cb) {
var xhReq = new XMLHttpRequest();
xhReq.open("POST", "ClientService.svc/REST/GetDetail", true);
xhReq.onreadystatechange = function() {
if (xhReq.readyState == 4) cb(xhReq.responseText);
}
xhReq.send(null);
}
// to call:
getDetail(function(data) {
JSON.parse(data);
}
Second, your problem is not that JSON is being parsed incorrectly. It's your debugging call to alert. When you pass the serverResponse object, alert coerces the object into a string by calling the object's toString method, which simply returns '[object Object]'.
Try console.log. Objects can be inspected in the console.
It actually sounds like this is working. If you call some thing like this:
alert(JSON.parse(serverResponse))
It will display [object Object] which is correct. If you call
alert(JSON.parse(serverResponse).appName)
You should see the appName. If you are not seeing "SyntaxError"s being thrown, JSON.parse() is working
Your JSON format is wrong and the data needs to be a string.
So, this will work (I broke the lines to improve readability):
var data = "{" +
" \"AppName\": \"TheName\", " +
" \"AppUrl\": \"https:\/\/app\/icons\/unknown.png\", " +
" \"aGUID\": \"45c055d2-2edc-d4444\", " +
" \"DateCreated\": \"8\/23\/2012 11:04AM\", " +
" \"foo\": { " +
" \"ID\": \"yser123\", " +
" \"Name\":\"User\"" +
" }" +
"}";
var obj = JSON.parse(data);
alert( obj.AppName );
Of course, if you use simple quotes as string delimiter, the code would be:
var data = '{' +
' "AppName": "TheName", ' +
' "AppUrl": "https:\/\/app\/icons\/unknown.png", ' +
' "aGUID": "45c055d2-2edc-d4444", ' +
' "DateCreated": "8\/23\/2012 11:04AM", ' +
' "foo": { ' +
' "ID": "yser123", ' +
' "Name":"User"' +
' }' +
'}';
This not works:
var data = "{" +
" 'AppName': 'TheName', " +
" 'AppUrl': 'https:\/\/app\/icons\/unknown.png', " +
" 'aGUID': '45c055d2-2edc-d4444', " +
" 'DateCreated': '8\/23\/2012 11:04AM', " +
" 'foo': { " +
" 'ID': 'yser123', " +
" 'Name': 'User'" +
" }" +
"}";
jsFiddle: http://jsfiddle.net/9pmdm/1/

Categories