Jquery.each array push is not working? - javascript

I have this code:
var positions = [];
$('.category-description TABLE TD').each(function() {
var fulltxt = $(this).html().replace(/(<([^>]+)>)/ig,"");
var lengt = fulltxt.length;
var indx = $(this).index();
positions.push[fulltxt];
alert(positions);
});
I can't understand why it's not working.. Table always have atleast 3 TD's and fulltxt have content. Alert(positions) returns empty result.

It does not work because of a typo
positions.push[fulltxt];
^ ^
should be
positions.push(fulltxt);
^ ^
And it appears you are trying to reinvent $(this).text().
You can also use map()
var positions = $('.category-description TABLE TD')
.map(function() {
return $(this).text();
})
.get();

Related

Jquery check if data exist as a substring on an html table

Check if data exist on an html table even if it is a substring.
var substring = 'find me'; //substring or data that you wanted to check
var dataExist = [];
$('table#tableID tr').each(function () {
var arrayOfThisRow = [];
var tableData = $(this).find('td');
if (tableData.length > 0) {
if($(this).text().indexOf(substring) == -1)
dataExist.push(true);
}
});
console.log(dataExist.includes(true))
If you're just trying to figure out if a table has a string anywhere in it, you could simply use indexOf() on myTable.innerText.
var myTable = document.getElementById('myTable');
var exists = myTable.innerHTML.indexOf('find me');
If you want to make sure the exists variable is always a boolean, you can use the below line.
var exists = !! ~ document.getElementById('tableID').innerHTML.indexOf('find me');

Replace n patterns in a string

Is there an easy way to fix this code:
title_1 = $(this).closest('tr').find('td').html();
title_2 = $(this).closest('tr').find('td').next().html();
title_3 = $(this).closest('tr').find('td').next().next().html();
question = question.replace(/{title_1}/g, title_1);
question = question.replace(/{title_2}/g, title_2);
question = question.replace(/{title_3}/g, title_3);
So it isn't so dully (repeated) and can cover n occurences of title_ pattern?
I'm a beginner Javascript developer and a complete regular expressions newbie (actually, they scare me! :|), so I'm unable to do this by myself. I've tried to look for an inspiration in different languages, but failed.
You can use a function in the replace, to get the value depending on what you find:
question = question.replace(/{title_(\d+)}/g, $.proxy(function(x, m){
return $(this).closest('tr').find('td:eq('+(m-1)+')').html();
}, this));
Demo: http://jsfiddle.net/n3qrL/
String.prototype.replace() could take a function as second parameter.
var $this = $(this);
question = question.replace(/\{title_(\d+)\}/g, function(match, n) {
return $this.closest('tr').find('td').eq(n - 1).html();
});
Demo Here
Try this ,
Generalized for getting all td tag's text value :
$("table").find("tr").each(function(){
$(this).find("td").each(function(){
alert($(this).html());
var txt=$(this).html();
//var pattern="/{"+txt+"}/g";
//question = question.replace(pattern, txt);
});
});
NB. In your question you have not mentioned the value for 'question' . please define value for 'question'
It seems to me that you want to get the text content of the first three cells of a table row and use it to replace the content of a string, and that this is an element somewhere in the row. So you can do:
var n = 3; // number of cells to get the text of
var textArray = [];
var tr = $(this).closest('tr')[0];
var reString;
for (var i=0; i<n; i++) {
reString = '{title_' + (i+1) + '}';
question = question.replace(reString, tr.cells[i].textContent);
}
If you wish to avoid jQuery's closest, you can use a simple function like:
function upTo(el, tagName) {
tagName = tagName.toLowerCase();
do {
el = el.parentNode;
if (el.tagName && el.tagName.toLowerCase() == tagName) {
return el;
}
} while (el.parentNode)
}
then:
var tr = upTo(this, 'tr');

jQuery Split dropdown value

Help needed to split the dropdown value and put them to the inputs in two different tables.
$(document).on('change', '[id^="SelID_"]', function () {
SplitSelectValue($(this));
});
function SplitSelectValue(obj) {
var data = obj.find("option:selected").val();
var arr = data.split('|');
var tr = obj.closest('tr');
tr.find("[id^='SelVala_']").val(arr[0]);
tr.find("[id^='SelValb_']").val(arr[1]);
tr.find("[id^='SelValc_']").val(arr[2]);
tr.find("[id^='SelVald_']").val(arr[3]);
tr.find("[id^='SelVale_']").val(arr[4]);
}
jsfiddle
Modify your function like this:
function SplitSelectValue(obj) {
var data = obj.val();
var arr = data.split('|');
var tr = obj.closest('tr');
tr.find(".SelVala_").val(arr[0]);
tr.find(".SelValb_").val(arr[1]);
tr.find(".SelValc_").val(arr[2]);
var table2 = $('#AddFieldsToDebugDiv');
var row = table2.find('tr:eq(' + tr.index() + ')');
row.find(".SelVald_").val(arr[3]);
row.find(".SelVale_").val(arr[4]);
}
Values SelVald_ and SelVale_ go to corresponding row of the second table. Also I changed ids to classes because id cannot be duplicated.
Demo: http://jsfiddle.net/BE5Lr/3745/
Try replacing tr.find with $
function SplitSelectValue(obj) {
var data = obj.find("option:selected").val();
var arr = data.split('|');
//var tr = obj.closest('tr');
$("[id^='SelVala_']").val(arr[0]);
$("[id^='SelValb_']").val(arr[1]);
$("[id^='SelValc_']").val(arr[2]);
$("[id^='SelVald_']").val(arr[3]);
$("[id^='SelVale_']").val(arr[4]);
}
Gave me the expected results

I am inserting values in the array, then I am displaying that value to the textarea, in this array I need to remove duplication names?

in this SkillIds array if i found duplication name then i need to remove that names..
var SkillIds = [];
$('#SkillSets table tr :checked').each(function () {
SkillIds.push($(this).data("id"));
});
$('#textarea').val(SkillIds.tostring());
Try $.unique()
Sorts an array of DOM elements, in place, with the duplicates removed.
$('#textarea').val($.unique(SkillIds).tostring());
Or you can use $.inArray()
$('#SkillSets table tr :checked').each(function () {
var data = $(this).data("id");
if ($.inArray(data, SkillIds) === -1) {
SkillIds.push(data);
}
});
You can do the following:
var SkillIds = [];
$('#SkillSets table tr :checked').each(function () {
var id = $(this).data("id");
if( SkillIds.indexOf(id) == -1 ){//if element is not in array
SkillIds.push(id);
}
});
$('#textarea').val(SkillIds.toString());//typo here tostring() should be toString()

JavaScript - Find and replace word in array

How would I find a word (in this case a placeholder, e.g _ORGAN_) in an array and replace it with an element's value?
sql = new Array();
$('#system').change(function(){
filter = " topography_index = _ORGAN_";
sql.push(filter);
});
In this case I would want to replace _ORGAN_ with $('#organ_menu').val();
Try this:
// sql array
var sql = ['organ not found', '_ORGAN_ is here'];
var val_to_replace = '_ORGAN_';
var replace_with = 'heart'; // temp value - change it with $('#organ_menu').val()
$.each(sql, function (key, val) {
// search for value and replace it
sql[key] = val.replace(val_to_replace, replace_with);
})
console.log(sql)
JSFiddle: http://jsfiddle.net/d8sZT/
You can simply do by iterating the array and then assign the value to once it find its match.
for (i = 0; i < sql.length; i++) {
if (sql[i] === "_ORGAN_") {
sql[i] = $('#organ_menu').val();
}
}
example fiddle for better understanding.
You can simply iterate over the array and use replace on each element
var organValue = $('#organ_menu').val();
for (var i = 0; i < sql.length; i++) {
sql[i] = sql[i].replace("_ORGAN_", organValue);
}
var regExp = new RegExp(organ, 'g');
$.each(sql, function(index, value) {
sql[index] = value.replace(regExp, 'test');
})
I'd try something like this, using replace:
sql = new Array();
$('#system').change(function(){
filter = " topography_index = _ORGAN_".replace("_ORGAN_", $('#organ_menu').val(), "gi");
sql.push(filter);
});
You can do this:
First find the index of the item:
var index=sql.indexOf("_ORGAN_");
Then insert your new item at that index and remove the first one:
sql.splice(index,1,newitem);
splice

Categories