I am wondering just how come the following is giving me hassle, it looks straight forward but I am not sure whats happening.
So I have a input box and I want a user to fill it in, every time i run the following
i get unidentified and I am not sure why.
<input type="numbers" name="price" class="sell" placeholder="$0.00">
Am using the following javascript
var price = document.getElementsByName("price")
alert(price.value);
var price = document.getElementsByName("price")[0];
alert(price.value);
document.getElementsByName will give you HTMLCollection (kind of array) as a return type. This is because a number of elements in your dom may share the same name. So if you have number of checkboxes having same name you can refere each checkbox with the array index.
You have several problems.
The one causing your problem is that getElementsByName returns an HTMLCollection (which is like an array), not a single element. You need to pull the first item off it before trying to access its properties.
var price = document.getElementsByName("price")[0];
Second, numbers it not a type of input. number (singular) is.
Third, if you give the example $0.00 then you are informing people that you expect them to type a value starting with a $ sign. You can't have a $ sign in a number.
Related
I have a value that I need to compare to the values in an object. The object is like:
[{"dbid":800,"MemberID":1460,"ID":1460,"Search":"TRUE","Year_Start":"2017","Year_End":2019,"Last_Name":"XXXX","First_Name":"XXX","Middle_Initial":"X","Suffix":"","Email":"","Program_Code":"CM","Pending":"","Initials":"OS","Include":"1","Exclude":"0","Authoring_Names":""}, ... ]
and again for 100's of names.
I want to create a search box that allows the end user to compare a name to the names in the list. So I want to send the last name of the comparing value to a function that will return most of the information such as First Name, Middle Initial, Last name, Program etc. The comparing value may or may not be in the list.
I've look at
Vue JS2 find array value by id
and it's close, but I want more information than one element. Also I saw that it maybe possible to filter an object in Veux, since I store that information in there.
To find all people with a certain last name, you should use filter as it's very similar to find only it returns multiple items in an array.
const found = people.filter(({ Last_Name }) => person.Last_Name == Last_Name);
Note that to check if no people have been found you need to check if length == 0 because an empty array is still truthy.
Updated version.
I had figured out my previous issue with it but am still unsure how to do the last part. I do not know how to get the number that goes inside of the bracket instead of getting the value that is stored inside of it. The array consists of 12 numbers and for example I am looking for the highest value which might be 20 and was entered in the 4th spot. I can only figure out how to get it to display the 20 but I want to display the 4 instead. It has user input so the number that should be displayed may vary and I am unsure how to accomplish this.
I only need the arrays number to be displayed for the highest and lowest values. I don't know if I am using the wrong kind of array for this not.
So you have a couple issues but easy fixes. This only goes to fix image 3 which is highestValue.
1.) When you are setting highestValue you are saying highest value is equal to [January, amountOfRain]. You need to have it say highestValue <- data[1,2]. This will point it to amountOfRain.
2.) You need to change the name of either your Call or your variable highestValue because it's conflicting.
Give those a try and let me know what you get.
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.
This has driven me "doo-lally" this afternoon!
A vendor (Zaxaa) uses a multi-dimentional form thus:
<form method="post" name="zaxaa" action="xxxx">
<input type="text" name="products[0][prod_name]" value="ABC">
<input type="text" name="products[0][prod_type]" id="pt" value="FRONTEND">
</form>
** This is my understanfing of how a multdimentional array is set up, and it seems to pass the variables to the server OK.
However, dependant on what other inputs are set to on the test form, the [prod_type] (and others) may need to change to "OTO" This is obviously going to be a javascript function, (but not the variant that starts with "$" on code lines ... whatever that type is!)
I have tried
document.zaxaa.products[0].prod_type.value
document.getElementById('products[0][prod_type]').value
document.getElementsByName('products[0][prod_type]').value
but in everycase, I get "products is not defined". (I have simplified the form as there are ten product[0] fields)
I've solved it... mainly a glaring error on my part. The getElementById worked fine ... except in my test script I'd used getElementById[xxx] and not getElementById(xxx)!! ie "[" rather than "(" Does help if you get the syntax right!
But I will take notice of those other methods, such as enclosing both array arguments in ["xxx"].
getElementById didn't work because the only one of those elements that has an id is the second input, with id="pt".
On any modern browser, you can use querySelector to get a list of the inputs using a CSS selector:
var nameInput = document.querySelector('input[name="products[0][prod_name]"]');
var typeInput = document.querySelector('input[name="products[0][prod_type]"]');
Then use their value property. So for instance, to set the name to "OTO":
document.querySelector('input[name="products[0][prod_name]"]').value = "OTO";
Use querySelectorAll if you need a list of relevant inputs, e.g.:
var nameInputs = document.querySelectorAll('input[name="products[0][prod_name]"]');
Then loop through them as needed (the list as a length, and you access elements via [n] where n is 0 to length - 1).
Re
* This is my understanfing of how a multdimentional array is set up...
All that HTML does is define input elements with a name property. That name property is sent to the server as-is, repeated as necessary if you have more than one field with that name. Anything turning them into an array for you is server-side, unrelated to JavaScript on the client. (The [0] is unusual, I'm used to seeing simply [], e.g name="products[][prod_name]".)
You can access it in this syntax:
document.zaxaa['products[0][prod_name]'].value
document.zaxaa.products[0].prod_type.value
The name is a single string, not making a nested structure to access the input. It would need to be
document.zaxaa["products[0][prod_type]"].value
// or better:
document.forms.zaxaa.elements["products[0][prod_type]"].value
The complicated name does only serve to parse the data into a (multidimensional) array on the server side, but all data will be sent "flattened".
document.getElementById('products[0][prod_type]').value
The id of your input is pt, so this should work as well:
document.getElementById("pt").value
document.getElementsByName('products[0][prod_type]').value
getElementsByName does return a collection of multiple elements - which does not have a .value property itself. Instead, access the first element in the collection (or iterate it completely):
document.getElementsByName('products[0][prod_type]')[0].value
I'm attempting to fetch some numbers from an output in a select box and, .each over all of the selects every time I .change() the select box, and keep a running total of my amounts.
I'm missing something here in my code.
Here is my working example thus far.
http://jsfiddle.net/HdaMQ/
It's working partially. I'm failing to understand how to get both of the boxes selected values and add them. (there could be more than 2 available to the user)
I'm not too savvy with using regex, so I'm slicing and dicing my strings until I get the exact value I need.
The ultimate goal here, is if a user changes their select options, the price will update accordingly and know exactly how much they will be spending.
Put your values in properly, then changed your option select function to something like this:
var sum = 0;
$("option:selected").each(function(index) {
//alert($(this).prop("value"));
sum += parseFloat($(this).prop("value"));
alert(sum); //this alerts for each iteration, so you'll want to remove it
});