using jquery find on result set of find - javascript

When I do a find look-up on my table for hidden fields, I am seeing my two hidden fields. However, I want to further refine these 2 fields by their IDs. I notice that when I use find on the entire table using the "contains" that I get my 2 fields. However, if I do a find on the find results from the hidden fields, it returns an empty set. Can anyone explain why this is the case?
var table = sender.parentNode.parentNode.parentNode.parentNode;
// this finds my 2 hidden fields
var hidden_fields = $(table).find("input[type='hidden']");
// this finds each of the 2 fields individually by ID
var my_id_fieldA = $(table).find("[id*='hfMyIdFieldA']");
var my_id_fieldB = $(table).find("[id*='hfMyIdFieldB']");
// but this returns an empty set
var my_id_fieldA = $(hidden_fields).find("[id*='hfMyIdFieldA']");

You're looking for the filter function, not find. find selects child elements while filter filters the current selection.
Also there's no reason to find the table like that... try something like this.
var $table = $(sender).closest("table")
, $hidden_fields = $table.find("input[type='hidden']")
, $my_id_fieldA = $hidden_fields.filter("[id*='hfMyIdFieldA']")
, $my_id_fieldB = $hidden_fields.filter("[id*='hfMyIdFieldB']")
, $my_id_fieldA = $hidden_fields.filter("[id*='hfMyIdFieldA']")
;

Related

How to detect an exist element in an array with one or more elements

Here is the code in Jsfiddle
It's a simple form to create a new form : label and it's input
I already did four validations :
if input is empty
if there in no label before adding an input
if the input id is not matched the label
But there is a new problem : if I enter an input id which already exist, it will add a second input for the same label,
For example :
I created two labels and their inputs
if I enter "a" in "input id" again, it will add a second input to label "a"
And if I add a same label again, then for adding the input is the same problem, it will add to both of them.
To avoid this problem, I think that I need to check : if the input value is matched any existing input id, so don't add the input, the same for label
I try to do this :
create an empty array
var inputExist = [];
push the id in this array every time I add an new input inputExist.push($('#input_id').val());
do a loop for in the else to detect if the input value matched any element in the array
for (var i = 0; i < inputExist.length; i++){
if($('#input_id').val() == inputExist[i]){}
}
Scince the if condiction is based on the loop, I have to put the if inside the loop, which means the else too, so that will cause the adding input order do array.length times, not good!
I have no idea how to separer the detection and the instruction followed.
Any ideas?
Create an array where you store the labels you already added (or the values);
var labelsArray = [];
var valuesArray = [];
Then when you handle the button click, add the following check:
var currentLabel = $('#lable_name').val();
if(labelsArray.indexOf(currentLabel) > -1){
return;
}
In alternative you can use the jQuery function:
$.inArray(currentLabel, labelsArray)
Do the same with the values and you will be able to avoid duplication.
In alternative you can use jQuery to search for an input

How can I get Index from Picker value selected?

I'm building an app with Appcelerator.
So I have used a Picker component to display a list of value.
Now I want to know what is the index of element that the user have selected.
So I'm try to do this:
var indexRow =$.comboDecription.getSelectedRow(0).getZIndex();
but I have undefined value.
You can use the following code:
// first get all columns
var columnsArray = $.comboDecription.getColumns();
// since it is a single column picker, so first index column's rows will be the ones you need.
var allRows = columnsArray[0].rows;
// get the title of first row, getSelectedRow(index) takes column index which is 0 in this case
var currentRowTitle = $.comboDecription.getSelectedRow(0).title;
// get the titles of all rows, these titles will be used to get the index of current title.
// use underscore library 'map' method to iterate over all rows and get their titles in an array
var allRowsTitles = _.map(allRows, function (row) {
return row.title;
});
// *** OR *** you can use underscore _.pluck method
var allRowsTitles = _.pluck(allRows, 'title');
// finally, this is the index of the selected picker row.
var currentSelectedRowIndex = allRowsTitles.indexOf(currentRowTitle);
I understand that it is the long process, but there are other ways too which depends on your implementation process. Nevertheless, I have shown you the stuffs you can do at run-time, and so you can do other things related to pickers.
gZindex() returns the layer where the view is positioned.
The change event return the current selected index.

Datatable : add row and check added row's index

I want to check whether the newly added row appear in first page or not. One way of doing it is get the index, but I wonder why it doesn't work, the index for me is not accurate at all.
function add_row(name, time_taken, attempts) {
var t = $('#dashboard').DataTable();
var node = '';
node = t.row.add([
'',
name,
time_taken,
attempts
]).draw().node();
$(node).attr('id', concatSpaces(name)).hide().fadeIn('slow');
var index = t.row('#' + concatSpaces(name)).index() // doesn't work
any thought? stuck for 2 hours long!
API method row().index() returns internal index which doesn't mean row position in the table based on current sorting column and method.
You need to use the code below instead to locate index of the row based on current sorting column and method:
var index = table.$('tr').index(node);
See this jsFiddle for code and demonstration.

Add/Remove Extjs combo value

I am newbie to extjs.I need to add/remove some values from extjs combo box base on some condition. I tried following code but no luck.
var obj =Ext.getCmp('filter');
var myArray=new Array();
myArray['id'] = 'a';
myArray['value'] = 'a';
var rec = new Ext.data.Record(myArray);
//obj.store.add(rec);
obj.store.removed(rec);
}
Use getById to find record for remove.
var combo = Ext.getCmp('filter');
combo.store.remove(combo.store.getById('a')); //typo: sotre corrected to store
combo.store.remove(combo.store.getById('a'));
obj.store.remove(rec);
removed is not a store function.
removed is a buffer array in which all removed recors are added.
if you're going to have a big store, you should keep this array empty, because removed objects are stored during all the session.
if the combo didn't change try to add store.sync() after adding or removing records

Javascript: how to get values from all selected checkboxes from a table

I've searched in many threads but i don't see any checkboxes that involves tables, i just wanted to make deleting easy for users, Say, i have a table with fifty entries and i wanted to delete 10 all in one go, so i select the check box beside each record so when i want to delete it it will just get the values of the checkbox pass it on to a php script..
My question is how do i im[plement this on Javascript or jQuery?. getting the values from n numbers of checkboxes? since tables have a dynamic value depending on how many it has on the database.
Here is an image to be clear:
This will give you an array containing the value attribute of each checked box:
var values = $('input:checked').map(function() {
return this.value;
}).get();
See http://jsfiddle.net/p58Hw/1/
Select your table, find all rows with a checked checkbox, store their values, and remove them:
var $chkboxes = $(yourTable).find("tr input[type='checkbox']:checked");
var checkBoxVals = $chkboxes.map(function(){ return $(this).val(); }).toArray();
$chkboxes.closest('tr').remove();
// serialize array checkBoxVals and pass it to your php script
You would post your code. But try something like this:
$('.YourCheckboxClass:checked').each(function() {
// your code here
});

Categories