I am trying to get the input elements in a form using JavaScript. Then I am trying to organize it by putting it into an array by its name attribute. Can anyone please help?
function getFields(formName, attr){
var form=document.getElementById(formName);
var fields=form.getElementsByTagName('input');
var fieldCount=fields.length-1;
var fieldNames=[];
for(var a in fieldCount){
fieldNames[a]=fields[a].getAttribute(attr)
}
return fieldNames[1];
}
It's already in an associated array via the DOM object:
var form = document.getElementById("myForm");
form.elements['myInputName'] //Returns <input name="myInputName">
form.elements['myInputName'].value //Returns the value
Related
I would like to get the values of dynamically created hidden fields with a class reference.
Example of created hidden field
<input class="SelectedClaimants" id="CodesList_2__Claimant" name="CodesList[2].Claimant" type="hidden" value="Jason Statham">
This is something along the lines of what i have tried.
$('.listSelected').on('DOMSubtreeModified', function (event) {
$(".SelectedClaimants").find('input[type=hidden]').each(function () {
var testC += $(this).val();
});
});
I was aiming to have them create into an array object, but at the moment i am happy just to get the values out into a concatenated string.
Try this (the result is logged to the console). It's based onn Tushar's answer, but the selector was wrong.
$('input[type="hidden"].SelectedClaimants').map(function () {
return $(this).val();
}).get().join(',')
You can use .querySelectorAll(), spread element, for..of loop. Note, id, e.g., CodesList_2__Claimant should be unique in document.
var testC = [];
for (let el of [...document.querySelectorAll("input[type='hidden'].SelectedClaimants")]) {
testC.push(el.value)
}
I'm trying to access form data through a Javascript function when an input is 'changed'.
Currently I use something like this:
<form>
<input type="radio" name="type" name="myValue" onchange="myFunction(this.form)>
</form>
<script>
function myFunction(form) {
var value = form.type.value;
}
</script>
And it works, however instead of writing
var value = form.type.value;
I need to write something like this
var myArray = ["type"];
var value = form.myArray[0].value;
Which is not working. Is this due to how values in arrays are handled?
I have it here on JSFiddle if that is useful.
Thanks
Try
var value = form[myArray[0]].value;
form.myArray[0] is first getting the member myArray from form, and then trying to get the first item in that. Equal to (form.myArray)[0]
form[myArray[0]] explicitly says get the member from form which is of the name of the value inside myArray[0]
You can access properties of object using [] like this:
var value = form["type"].value;
// equivalent to:
var value = form.type.value
In your case this should work:
var myArray = ["type"];
var value = form[myArray[0]].value;
'myArray' is declared as a local variable but it is NOT form's property.
So form.myArray will fail.
I am new in jQuery and I would like to know how to save/access separately each extracted word from the filter function.
var str="<b>I'm</b> a great <b>chef</b>.";
var extract=$(str).filter('b').text();
alert(extract);
//Output: I'm chef
I want to store in a different variables the I'm and chef
Try to use .eq() for accessing the element collection,
var str="<b>I'm</b> a great <b>chef</b>.";
var extract=$(str).filter('b');
var var1 = extract.eq(0).text(); //I'm
var var2 = extract.eq(1).text(); //chef
Or you can extract those values in an array like,
var arr = extract.map(function(){ return $(this).text(); }).get();
I couldn't find an answer to this problem with submitting an array of arrays when i select multiple checkboxes. I am able to pass it to my struts action but it is just considered as a single object. I was expecting multiple objects in a list but i only get one. The console return a value of 1 for x (always). Can anyone help in correcting this code, please?
thanks again.
here is my jquery code:
var checkosbrowser=$('#oidtable').find('input[type=checkbox]');
var selDataList = [];
var vaOSBrowser;
for(var i=0;i<checkosbrowser.length;i++){
vaOSBrowser = [];
if(checkosbrowser[i].checked){
var p = checkosbrowser[i].parentNode.parentNode;
var id=p.id;
vaOSBrowser.push($('#oidtable').jqGrid('getCell',id,'environment'));
vaOSBrowser.push($('#oidtable').jqGrid('getCell',id,'onlineID'));
vaOSBrowser.push($('#oidtable').jqGrid('getCell',id,'releaseDate'));
selDataList.push(vaOSBrowser);
}
}
alert("array list is " + selDataList.toString());
var selData = document.getElementById("selectedDataList");
selData.value = selDataList;
I am facing problem in java-script problem as
I have to collect all subrole from screen whenever user focus out from input field.this is my code
var subrole_id = [];
$("ul :input[class^='sub']").live('focusout', function() {
var get_id = $(this).attr('class').split("_");
var ids = get_id[2];
var ids_index = get_id[3];
subrole_id[ids][ids_index] = this.value;
});
but this is giving error
TypeError: subrole_id[ids] is undefined
subrole_id[ids][ids_index] = this.value;
Actually I want to collect value of subrole input field and add them in array with index ids one by one and before adding that value in array I have to check whether that present value is present in array or not if yes then do not add and give error.
Please suggest.
There isn't a true multidimensional array in javascript, just an array of array, so you have to create the subArray:
subrole_id[ids]=subrole_id[ids]||[];
subrole_id[ids][ids_index] = this.value;