jquery - add to array - javascript

Why this not working?
var inputs = new Array();
$("input").each(function(){
input = $(this).val();
})
console.log(input);
how to correctly use arrays in jQuery? Like as PHP?

I assume what you are trying to do is get an array of the values of all the <input> elements on your page. What you'll need to do is iterate over all the elements using the .each() function and append each value to your inputs array.
Try this -
var inputs = new Array();
$("input").each(function(){
inputs.push($(this).val());
})
console.log(inputs);
You need to use the push() function to add an element to an array.
fiddle demo
References -
Array push method on MDN
As a final note, here is a shorthand way to define a new array -
var inputs = [];
That line is functionally identical to -
var inputs = new Array();

Use Array.push
var inputs = new Array();
$("input").each(function(){
inputs.push($(this).val());
})
Also note the variable differences .. input != inputs

Related

mongodb multiple find and replace old value with new value in any field

I want to find and replace (update) the values of any field in all my documents with the following pattern:
{[<oldValue1>:<newValue1>,<oldValue2>:<newValue2>,<oldValue3>:<newValue3>]}
i have hundreds of this kind of old/new values, and they are spread over multiple fields, I want to look for them wherever they are in the fields, and update them with their corresponding new value.
how can I achieve this?
Try this solution.
var cursor = db.test.find();
while (cursor.hasNext()) {
var x = cursor.next();
var arr=[{"oldvalue":"newvalue"},{"oldvalue":"newvalue"}];
for(var i=0;i<arr.length;i++){
var keys=Object.keys(arr[i]);
print("Before: "+keys[0]);
x[keys[0]] = x[keys[0]].replace(x[keys[0],arr[i][keys[0]]);
print("After: "+keys[0]);
db.test.update({_id : x._id}, x);
}
}

jQuery Saving different texts in the same tag extracted using jquery filter

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();

adding input value in multidimensional array?

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;

Get inputs value and separated by "|"

I have inputs and I need to get values and separated by "|" symbol.
My input:
Output what I need:
00:00|00:00|00:00
My code is :
(and it's not working)
var timesArray = $('table').find('input');
var times = timesArray.val();
$('.alltimes').val(times);
Given that .val returns the value of each element (whatever input type it is in your screenshot), then you can use .map: http://jsfiddle.net/RrCYD/.
var values = $("input").map(function() {
return $(this).val(); // map each element to its value
}).get().join("|"); // get real array and join
See comments in code:
// Create an array to store the input values
var inputValues = [];
// Iterate over input's and store their value in the array
$('table').find('input').each(function () {
inputValues.push(this.value);
});
// Create pipe=delimited string from array of values
var delimitedInputValues = inputValues.join("|");
Additional Information
MDN - array.join
jQuery - .each()

Does jQuery have a collect method

I have this code
var selected = new Array();
$(".client_associates_users input:checked").each(function(index) {
selected.push($(this).val());
});
and it works great but i was wondering if jQuery had a collect so i would not have to create an array and push to it.
I basically want all the values of the checked input fields
jQuery offers the .map() method,
var selected = $(".client_associates_users input:checked").map(function(index) {
return this.value;
}).get();
This method will create a new array out of whatever data you return from within the callback. Be aware that you need to call .get() at the end to get a real array.
Ref.: .map()
Yes, $.fn.map http://api.jquery.com/map/ or $.map http://api.jquery.com/jQuery.map/
var $checkboxes = $(".client_associates_users input:checked"),
selected = $checkboxes.map(function(i){
return $checkboxes.eq(i).val();
}).get();
or this for $.map (I prefer this one)
var $checkboxes = $(".client_associates_users input:checked"),
selected = $.map($checkboxes, function(el,i){
return $checkboxes.eq(i).val();
});
Update
No longer recreates a jQuery object on each iteration.
The jQuery object $(".client_associates_users input:checked") is an array so you should just be able to access those values if you append a .val() to the end of that selector.

Categories

Resources