Find default value of field after page refresh - javascript

I'm looking for a way, with jQuery, to get the default value of an input field (in an HTML field).
Seems simple enough. Just calling $("#id-of-field").attr('value') should return the value I'm wanting right?
Thing is, if the value is Foo, when I load the page, it returns Foo. No problem there. But if I write Bar in the field, then reload (not submit) the page, $("#id-of-field").attr('value') will return me Bar, even if in the source code, the value attribute of the field is still Foo.
That makes validation of my form sticky, because when I get my default values to ignore them if the field has not been filled, I could get a real value in the mix of "Values to be ignored".
FYI, I can't put in the values manually, since the form is dynamic, and cannot query directly the database because it's a Wordpress Website.

Input fields have the defaultValue DOM property that does exactly what you need.
According to this answer, the jQuery (> 1.6.3) way to access it is
$('#element').prop('defaultValue')
Alternatively, in pure JavaScript, e.g. -
document.getElementById("element").defaultValue

Try this
$('inputSelector').prop('defaultValue');

You could try -
$("#id-of-field")[0].defaultValue
That should get the DOM defaultValue property which should give you the original value of the textbox
https://developer.mozilla.org/en/XUL/textbox#p-defaultValue

Related

D365 CustomJS - Update Field with AutoNumber Field OnSave

I have a Autonumber field called 'ID' that generates an ID for a record. I want to set the Primary Name field called 'ID_Name' to this. I am currently using the following JS:
function setName(executionContext) {
formContext = executionContext.getFormContext();
var name = formContext.getAttribute("id").getValue();
formContext.getAttribute("id_name").setValue(name);
}
Pretty simple. I get the value of 'ID', assign it to a var called name, then set the value of 'ID_Name' to that var. This triggers OnSave.
This works fine when editing a record. The problem is, this does not work when creating a new record. I assume because at the time the OnSave triggers, Autonumber field 'ID' hasn't generated a value which can be used yet, so ID_Name is set to blank. Of course while editing, ID has a value because the record has already been submitted, so no problems. Is there a way around this issue?
Just set the OOB autonumber option on the primary name field of your entity. No code required and it saves you from duplicating data, which should be avoided when possible.
You can do some workaround using the OnLoad event and trigger an update when there is a mismatch between the two fields (meaning the value has not been saved) but I strongly suggest to don't use this kind of approach.
Ideally this can be solved using a synchronous plugin inside a post operation, at this moment in the pipeline the id field should be filled (you didn't mention if you are using the OOB autonumber or something different to generate the content of this field) and you can trigger the update inside a plugin.

getting empty field value with JavaScript in FormAssembly calculated field

I'm pretty inexperienced with JavaScript, but I'm trying to do something that seems like it should be simple. I have a text input field (FieldA) in a FormAssembly form. I have another text input field that’s a calculated field (FieldB), and I want FieldB to return one value if FieldA is empty and a different value if FieldA is not empty. The formula I’m using in “Enter the calculation” looks like this:
if(FieldA==""){"empty"}else{"not empty");
When I go to the form, the result is “error” in FieldB, and adding a value to FieldA has no effect. When I do it in Preview mode, I get the additional message “There was an error computing this field.” FieldA here is tfa_40, but it seems to make no difference whether I use tfa_40 or define it as a variable and use the variable name. It also doesn’t seem to matter whether I use double quotes, null, or 0. Am I taking the wrong approach here? Is this even possible?

Access values of fields using form controls in javascript?

I have a form with some text fields(mostly date fields) disabled for a particular user,but has got some value in it.I am not sure whether it is a read-only field.
anyway the thing is I want to access the disabled field value to perform some other operation using javascript
Here's the stringified value of the control
{"idbase":"row-4-cell-3","lock":{"vary":0,"fixLock":1,"hidden":"","epmSourced":1},"desc":"Plan_date_ac","name":"date-4-3","lockValue":null,"type":"AC"},
I tried using:
form[var.control).value
and:
document.getElementById(controls[var].idbase).innerHTML
neither works for me, it keeps throwing 'undefined'.
Note: only javascript, no jquery please!
That's a class. To get class data with Javascript. Used below code
var x = document.getElementsByClassName("example");
x.value // to get the x value
x.innerHTML // to get the content inside the element
Refer:
https://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp

dijit.byId('viewEditParameterValue').value does not return while .get('value') does

I have defined two Text input in my html like this..
<revit:validationTextBox id="viewEditParameterValue" maxLength="2500"
required="true"
invalidMessage="#{commonuielements.msg_invalid_input}">
</revit:validationTextBox>
<revit:validationTextBox id="viewEditParameterValDefault"
maxLength="100"
regExp="#{commonuielements.parameter_default_value_regex}"
invalidMessage="#{commonuielements.msg_invalid_input}"
trim="true"></revit:validationTextBox>
I am trying to get the value of two TextBox in a java script function like this..
var value = dijit.byId('viewEditParameterValDefault').value;
var parValue = dijit.byId('viewEditParameterValue').value;
But for the first one I get the value but second line returns blank whereas If I use below line I get the value.
var parValue = dijit.byId('viewEditParameterValue').get('value');
Does anybody have any guess what could be the reason?
get('value') is the correct way to retrieve the value of a Dijit form input widget. Accessing value directly isn't guaranteed to give you consistent results (or the results you expect), since it will be implementation-dependent. This is perhaps even more important for certain subclasses of TextBox where formatting or mapping come into play.
Moreover, setting value directly won't accomplish anything, whereas calling set('value', ...) will properly update the widget.

A JQUERY standard way of storing the original value of a form field?

Is there a standard way in JQUERY to (at document ready time) store the original values of the form fields on the page in order to check (later) whether the fields have truly changed or not?
Normally I will do something like this:
var NameField = $("INPUT[name='NameField']");
//Record the original value.
NameField.OriginalVal = NameField.val();
This is simple enough, but I'm wondering of there is a "Standard" way to do it.
EDIT
One added requirement that I forgot is that it needs to work for all types of form fields including select and textareas.
The default value (i.e. the value that was specified in the source code using the value attribute) is available automatically as the defaultValue property.
alert($("#myInput").defaultValue);
More info at W3Schools
This seems to be part of the JS spec since 1.0 so it's safe to use. (Reference, German only sorry)
There is no standard way to store original values.
jQuery does have a better way to store values than your example:
var NameField = $("INPUT[name='NameField']");
NameField.data('OriginalVal', NameField.val());
Another simple way is to set a boolean variable on input control's onchange event.
i.e. var isFormValueChanged = false;
$("INPUT[name='NameField']").bind("change", function()
{
isFormValueCanged = true;
});

Categories