How to get value of a element whose name is string1.string2 - javascript

In My web app I am using Struts 1.x, In one of my Jsp I need to validate some fields, I have used Struts 1.x html tags, for some reason I need to use nameing convention as below.
<html:form action="someAction">
<html:text property="Object.textId" />
</html:form>
What my problem here is when I want to validate that Text field in my JAva Script I am using
below Methodology
function validate()
{
var frm = eval("MyFormName");//The form name which I havve mentioned in Struts-config.xml
var ss = frm.Object.textId.value;// Here I cant get the value of my texte field it says it cant find textid
// error seems to be resonable so I have given below try but still it didnt work
var ss= frm.+'Object.textId'.value;
}
I need javascript to recognise my naming of my fields which contains dot . in it.
I can not change my fiekds names to some notation.
thanks In advance.
Edit
I need to pass my form fields names to some other validation fuctions, In these functions they are trying to get the value as below
var sss=eval(document.+'FormName'+.+'FieldName');
Thanks In advance.

Related

How to copy the value of a yform to an other field

In our (hybris) shop some products have a yform to summarize the parts of the product. Is there an easy way to copy the value of the sum field into an other field (automaticly) like the productquantity (no yForm)?
I guess I need javascript, but the id of the sumfield is generatad, so I don't know how to get the sum. Also my Javascript abilities are quite limited...
UPDATE:
To get the value I use this part of code:
copyYFormValueToProductQuantity : function() {
var copyText = document.querySelector('input[id*="sum"]').value
if (copyText > 0 && copyText != null)
{
//do stuff
}
console.log("Copied value: " + copyText)
},
But this line
document.querySelector('input[id*="sum"]').value
returns null. If I use it in the browserconsole, it also returns null. But after I inspected the element it works and returns the value I want to. So I guess I am missing some JS-basics here and the object isn't ready before?
Btw.: I call this function with a keydown-eventlistener.
This can most likely be done from the jsp files. There you have all the data that is needed, so you most likely need to only copy that field where you need it.
We can also help you more if you add some code examples here (what exactly is that yform?). If you struggle to find where in code is that specific yform created/added, it's always worth giving a try to search for the applied classes of the html (search the entire project and see what you find).
As I understand your question, you are saying that you want to copy the value of a yForm field named sum into a non-yForm field named productquantity, and you are asking specifically about how to access the value of a yForm field from JavaScript. If I understand this correctly, you can do so by calling the following JavaScript API:
ORBEON.xforms.Document.getValue("sum");
You can find more about this and other related API on Client-side JavaScript API.

Angular Js : How to get input value using AngularJS functions

I want to access form input value in services.
var emailId = document.getElementById("save_quote_email").value;
console.log("emailId="+emailId);
console.log("emailId2="+angular.element('#save_quote_email').val());
I am able to get value using document.getElementById("save_quote_email").value but not using angular function angular.element('#save_quote_email').val()
It gives error
Error: [jqLite:nosel] Looking up elements via selectors is not supported by jqLite! See: http://docs.angularjs.org/api/angular.element
http://errors.angularjs.org/1.2.16/jqLite/nosel
Please view this plunker http://plnkr.co/edit/Op1QDwUBECAosPUC7r3N?p=preview for complete code.
in dtoresource.js line numbe2 21.
You can't use angular.element to select by id or some other selector Link provide by you give full info as below
"Note: Keep in mind that this function will not find elements by tag name / CSS selector. For lookups by tag name, try instead angular.element(document).find(...) or $document.find(), or use the standard DOM APIs, e.g. document.querySelectorAll()"
you may use document.query selector as follow
var queryResult = document.querySelector('#save_quote_email');
var emailId2 = angular.element(queryResult);
Here is working plunker http://plnkr.co/edit/mC0JKTZpdnvqyBpihLRW?p=preview
Also this https://stackoverflow.com/a/23626251/5621827 will help you to understand it batter
Instead of trying to answer your question I am going to rather direct you to the correct way of using Angular.
You should never try and select input values, that is what ng-model is for.
ng-model bind the input to a value. As soon as the value changes Angular automatically updates the scope value, no functions of getting data necessary.
I can't make heads or tails out of your plunkr code but you should not do this:
var queryResult = document.querySelector('#save_quote_email');
Also you are trying to access the DOM from a factory, you should rather pass the needed value to an exposed function of the factory and call the function in your controller.
In your controller autoQuoteCtrl you should use a simply scope value for your input like so:
$scope.email = "";
And in your index the input should read:
<input type='text' ng-model="email" />
{{ email }}
Do not use convoluted variables like AutoQuote.postAutoQuoteObj.ApplicationInfo.GeneralPartyInfo.ContactInfo.Emails[0].EmailAddress in your scopes, you should assign the values when they are received from the resource.
$http({url: url}).then(function (rs) {
$scope.email = rs.postAutoQuoteObj.ApplicationInfo.GeneralPartyInfo.ContactInfo.Emails[0].EmailAddress;
}

Adobe Acrobat - Error Iterating Over All Fields in a PDF with JavaScript

I'm having trouble iterating over all of the fields in my document to remove the tooltip. Here's my code:
var index=0;
while(index<this.numFields)
{
var nom=this.getNthFieldName(index);
var fieldName=this.getField(nom);
fieldName.userName = "";
index=index+1;
}
I'm getting an error saying fieldName is null and my script won't run. I've seen this answer already:
Iterating over all fields in a PDF form with JavaScript
I get the same error with that code too. If I manually assign a field name to fieldName using var fieldName=this.getField("field1");, it works fine.
Does anyone have any idea why this would error on me?
Edit:
I can iterate over the list and output nom to the console so I know it's grabbing the names of the fields properly. It seems to have trouble dropping that name into the this.getField(nom) statement. No idea why...
Why use while… for this?
Doing exactly the same (setting the mousetip text to a blank string) is simpler using
for (var i = 0 ; i < this.numFields ; i++) {
this.getField(this.getNthFieldName(i)).userName = "" ;
}
and that should do it.
However, unless you have a very good reason, setting the userName to the blank string is not recommended; it is needed if your form is used with assistive devices, and it is also the very nearest and simplest help item.
I figured out my issue.
When I created the form, I used the automatic field detection to create my fields for me in order to save time (there are like 250 fields on this form). The reason I needed the script in the first place was to remove the crummy tooltip names that the feature generates.
Apparently, in its infinite wisdom, the field detection feature named a handful of fields with a leading space ( something like " OF INFORMATIONrow1"). Since getNthFieldName(index) returns the fields in alphabetical order, it was returning one of these broken fields and erroring immediately because getField() doesn't like the leading space in the name.
I renamed the handful of fields and the script works like a charm.

Check Starting Form Values For JavaScript Validation

I have an HTML form that I already validate using Javascript. However, I am only checking to make sure it has been filled out. I also need to check to be sure that the first eight characters of the form do not contain "FFFFFFFF" or "ffffffff". It will be a 40 character input into the form field. I am just not sure how to do this with Javascript.
If someone could point me in the right direction, I would really appreciate it.
var txt = document.getElementById('id').value;
var first = txt.substring(0,8);
if(first=="FFFFFFFF"||first=="ffffffff"){
// validation error
else
//validation success
as simple as that. i can't be more specific since you haven't shared any of your code.

Using jquery to send a javascript variable to rails variable on submit

So I have this javascript variable I'm generating on my html.erb views that I want to send to a rails variable the form submit. I understand that I can do this using jquery or ajax but my attempts have failed. Here is what I am trying:
Rails variable:
<%= f.text_field :antennatime, :id => "antenna_time" %>
Some HTML:
<input type="checkbox" id="Antenna" onclick="fun();" value="1">
My javascript:
<script>
var str = "This is Some Sample Text";
var ant = document.getElementById("Antenna").checked;
if(antenna == true){
$(document).ready(function(){
$("antenna_time").val(str);
});
}
</script>
To my expectations this is not working. Could someone show the steps I should use to use jquery or ajax to send my javascript var str to my rails variable :antennatime?
It looks like you've misspelled a variable and missed a # from your jQuery selector:
var str = "This is Some Sample Text";
var ant = document.getElementById("Antenna");
if (ant.checked){
$(function(){ // same as $(document).ready()
$('#antenna_time').val(str);
});
}
For clarity, using an if statement is for checking expressions/evaluating - I would add your .checked logic there as it's more clear what's happening.
Your question is a bit confusing. Let me paraphrase to make sure I understand: you have a form, of which one field is filled with a attribute of a model, and when a checkbox is set, you want to set the value of that field, using javascript.
So in reality, you are not setting some "rails variable" but you are changing one field in a form, before submitting it to the server.
So, in that case it is simple, you just omitted the # in your jquery. So you will have to write
$('#antenna_time').val(str)
This will set the field to the requested value. But as per your code, it will do this, only when the page is loaded and when the checkbox is clicked.
This might be what you want, but I would suspect you
want it to be set/unset when the checkbox is clicked? (the fun() function is not shown --speaking of cryptic names)
to be checked before submit only (possibly confusing for the user, and why not just check the value of the checkbox server-side instead then)
Please note, to set a rails variable you will have to post something to the server.
It is not entirely clear why you overwrite the id of the field, which imho is potentially dangerous, since rails uses the id/names to build the params hash to be posted to the rails server. The id of an input-field of an attribute in a model is always built as follows: <model-name>_<attribute-name> which is unique (and clear) enough imho to just use that.
You need to compare the ant variable not antenna since you've defined it above if condition as well as using # to target element with id named antenna_time
Also wrap all your code inside DOM ready, final code look like this:
$(document).ready(function(){
var str = "This is Some Sample Text";
var ant = document.getElementById("Antenna").checked;
if(ant == true){
$("#antenna_time").val(str);
}
});

Categories

Resources