Setting field focus after javascript alert not working in MS CRM - javascript

I have the following code in the OnChange() event for a field.
alert("alert text");
crmForm.all.fieldname.SetFocus();
The page acts like the SetFocus call isn't even there.
Anyone know why this is?
EDIT: I've also tried the following to no avail.
crmForm.all.fieldname.Focus();
crmForm.all.fieldname.focus();
alert("alert text", function() { crmForm.all.fieldname.SetFocus()});

In the DOM, the function to set focus on an element is called focus(), not SetFocus().

Turns out that retaining focus on the field from which the OnChange() method was called is broken in CRM 4 without the most recent rollup. This is a known issue with a Microsoft KB article.
To achieve the illusion of retaining focus on the field simply set the focus to a different field on the same tab first and then reassign the focus to the field from which the OnChange() event was called like so:
alert("alert text");
crmForm.all.some_other_field_on_the_same_tab.SetFocus();
crmForm.all.fieldname.SetFocus();

Seems like the same problem exists in CRM 2011 - event when working with Xrm.Page.
The workaround is still working:
Xrm.Page.getControl("name").setFocus(true);
Xrm.Page.getControl("TheFieldYouReallyWantToFocus").setFocus(true);

Related

CRM 2015 setValue() triggering onChange

I'm trying to use the built in setValue() funtion in CRM 2015 to change a field back to null if a confirm() command comes back as false. The field that triggers the confirm() is also the field I am trying to set to null.
The code is as follows:
if (optionSetField.getValue() == 805430000 /*YES*/) {
var tempBoolean = confirm("Test", "test Title")
if (tempBoolean == false) {
optionSetField.setValue(null)
}
The field I am trying to set to null is an option set field with "yes" and "no" as the available options. I found this preferable to use over a bit field as it meant I did not require a default value.
Upon the code triggering and the user clicking 'Cancel' the setValue(null) triggers the onChange event, which despite the field supposedly being set to null asks for confirmation again. Is there something I'm doing wrong or is this a potential bug? Is there an alternate suggestion for what I am doing?
According to the Microsoft tech docs setValue() should not trigger onChange events, but clearly this is not the case. Anyone's insight would be helpful!
See here for the docs: https://msdn.microsoft.com/en-us/library/gg334409.aspx#BKMK_setValue
You should use confirmDialog, since it's callback-based it should allow the field to update itself properly.
Xrm.Utility.confirmDialog(message, yesCallback, noCallback)
So like this
if (optionSetField.getValue() === 805430000) {
Xrm.Utility.confirmDialog("Test", function(){
optionSetField.setValue(null);
});
}
This would also work on tablets (confirm, on the other hand, won't)
I've had CRM 2015 U1 Turbo forms trigger onchange events, when it shouldn't be according the the setValue Documentation. CRM 2016 Update 1 appears to have resolved this issue for me, although I have been unable to find a bug fix listed anywhere in Microsoft's support documentation.
This is the active connect case: https://connect.microsoft.com/dynamicssuggestions/feedback/details/2527691

onchange wont fire after programmatically changing html select dropdown

I have a select inside HTML
<select id="league" name="league">
which I'm listening for changes inside my javascript.
var league = dojo.byId("league");
dojo.connect(league, "onchange", function (evt) { //do stuff }
Which works fine.
However I have a link that I can click which updates the select:
League
The link works as it updates the selected value of the select with the following function.
function updateSelection(NewLeague){
dojo.byId('league').value = NewLeague; // works
dojo.byId('league').onChange; //this isnt working
//dojo.byId('league').onChange(); //this throws: TypeError: dojo.byId("league").onChange is not a function
}
My problem, as I've read through other stack posts is that programmatically updating the value wont trigger onChange, thus I need to call onchange in the code (shown above). As per the comments inline, the onChange isn't being triggered or throws an error. My first thought that it has something to do with the dojo.Connect which listens for onChange, but I havent found any information that says I cant do this, nor any explanation how to get around it.
Any ideas?
Select onchange doesn't fire for programattic changes, you need to fire it yourself with league.onchange();
As noted by #Greg, the call should be lowercase.
Additionally, I don't know if dojo has a trigger method, but in jQuery this would be done as jQuery('#league').trigger('change').
Depending on your version of dojo you may also want to check: http://dojotoolkit.org/reference-guide/1.8/dojo/connect.html
Have you tried just calling the select by it's id using normal js?
document.getElementById('league').onchange.call();
As others have said, you need to trigger the event yourself, just setting the value does not do that. See the code on How to trigger event in JavaScript? to see how in a cross-browser way.

onchange not getting triggered

I have a calendar which comprise of an input tag and an image on click of image calendar popups comes and sets the value for input tag using javascript.
now i have added onchange event on input tag which doesnt gets triggered since m changing the value using javascript, so what should be done to trigger it i dont want to trigger it explcitly
i tried focusing on input on click of image and focus out on setting the data(into input element) but it didnt worked any workaround?
You can call the onchange handler manually.
Example: http://jsfiddle.net/NQCy7/
element.value = 'new value';
element.onchange();
just tested what you described, it's the same problem here.
a simple workaround would be to call the onchange-method directly (where you're doing the .focus()/.blur() now)

onchange event from select box to update field

On this page:
http://www.blackdownluxurylettings.co.uk/place_booking/2010-3-18,2
I am using an onchange event on the "number of days" select box to update the "departure date" field.
But currently nothing is happening.
Any ideas?
Just looking at it quickly: wouldn't you want the ONCHANGE event attached to the SELECT tag rather than the individual options?
You can bind it this way using JQuery:
$("#ddlNumberOfDays").bind('change', mainPharmacy_Change)
Maybe for javascript, you should just try 'change'.
If I recall correctly, the regular Javascript onChange event only fires once the select box loses focus AND it's contents have been changed. I don't know how jQuery achieves it, but their change method will fire whenever the contents are updated.
Also, I get the following error in my Javascript console when loading your page:
[cycle] terminating; zero elements found by selector

Cannot get Javascript function to fire from a textbox in a gridview

I have a textbox in the ItemTemplate of a Gridview. On the _RowDataBound event I add an attribute to the textbox like so:
TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
txtQuantity.Attributes.Add("onkeypress", "CheckInputForNumeric(event)");
And it simply will not fire a JS function.
I've tried doing onClick, onBlur, onKeyPress... even tried changing the case to: onclick, onblur, onkeypress... nothing seems to be able to fire my JS function.
elsewhere on that same page I have:
txtAddMarkup.Attributes.Add("onkeypress", "CheckInputForNumeric(event)");
(that textbox is not in a gridview)
and this works just fine.
I'm totally stuck and frustrated at this point because it seems no matter what I do, I cannot get this textbox to fire a JavaScript function
Please run your project and look at the name of the textbox generated by viewing the source in the broswer (IE, Firefox, Safari, whatever). You'll likely see that the name of the textbox has changed. Thanks, ASP.
You can't use the DOM to access the elements by name because they're renamed for you.
For some reason, deleting temporary internet files and reloading the page wasn't getting the newest .js. I had to unload the project and re-build it. Which is weird because I've never had to do that before for other controls
thanks for your inputs!
try this one:
TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
txtQuantity.Attributes.Add("onkeypress", "CheckInputForNumeric(event)");
txtQuantity.Attributes["onkeypress"] =
string.Format("javascript:CheckInputForNumeric(this,'{0}','{1}','{2}');", argument1, argument2, argument3);
Or Simply
txtQuantity.Attributes["onkeypress"] =
string.Format("javascript:CheckInputForNumeric (this);");

Categories