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');
Related
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();
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');
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()
here I am trying to write like this..
but this code is not working for me as i expects..
i am getting the alert message like
[{rcid:1}]
but i want alert message like this.. [{rcid:1, rdsid:10}]
Note: rcid is selected parent recordid and rdsid is selected child reord id
any ideas would be greatly appreciated.. thanks in advance..
function CheckForm() {
var $selectedRows = $('#PersonTableContainer').jtable('selectedRows');
// var $selectedChildRows = $('.jtable-child-table-container').jtable('selectedRows');
var myjson = [];
$selectedRows.each(function () {
var record = $(this).data('record');
myjson.push({
rcid: record.rcid,
rdsid: record.rdsid
});
});
$('jsondata').val(JSON.stringify(myjson)); // this will encode in a json string
alert(JSON.stringify(myjson));
return false;
}
I was trying to do something similar on my end. I was about to go crazy because I couldn't find the answer. I will first say, I'm new to jQuery so there might be a better way but I couldn't find it.
var $selectedRows = $('#PersonTableContainer').jtable('selectedRows');
returns a list of the selected rows from all the tables above it. In my case, I was 3 child tables deep. So my $selectedRows had a length of 3. I suspect yours has a length of 2.
For your situation I suggest:
var $rcid = 0;
var $rdsid = 0;
var $index = 0;
$selectedRows.each(function () {
var record = $(this).data('record');
if(index == 0){
rdsid = record.rcid;
}
if(index == 1){
rdsid = record.rdsid;
}
index++;
});
myjson.push({
rcid: $rcid,
rdsid:$rdsid
});
<table>
<tr><td>foo</td></tr>
<tr><td>bar</td></tr>
<tr><td>abc#yahoo.com</td></tr>
</table>
Can anybody tell me how to write a Javascript line to only grab the email address in the table below, I've been searching a lot, but all I come across is tutorials which use "id" in either table on in td .. I want to do it without having an id .. please help
var rows = document.getElementsByTagName("table")[0].rows;
var last = rows[rows.length - 1];
var cell = last.cells[0];
var value = cell.innerHTML
Try it yourself here: http://jsfiddle.net/ReyNx/.
Obviously you'll have to change document.getElementsByTagName("table")[0] to appropriately match your table
If you're using jQuery it's easier:
var value = $('table tr:last td').text();
For more info, see the MDN DOM reference, which shows you which properties are available on which elements to traverse the DOM.
No jQuery, innerHtml or other evil / heavy functions, just plain old JavaScript:
// Get the first table in the document.
var table = document.getElementsByTagName('table')[0];
// Get the third row of this table (0-index 3rd = 2)
var emailRow = table.rows[2];
// Get this element's content.
var emailContent = emailRow.firstChild.textContent;
You could write it in 1 line:
var emailContent = document.getElementsByTagName('table')[0].rows[2].firstChild.textContent;
If you want to find all email addresses in a table:
var emails = [];
var table = document.getElementsByTagName('table')[0];
var rows = table.rows;
for (var i = 0; i < rows.length; i++) {
var rowText = rows[i].firstChild.textContent;
if (~rowText.indexOf('#')) { // If the content of the row contains a '#' character (This could be replaced with a regex check)
// Also, I personally prefer to use '~' over '> -1' for indexOf(), but both would work.
emails.push(rowText);
}
}
console.log(emails);
Working example
If like me you want to get the text from all the first column items in all the tables on the page then use this.
jQuery('table tr td').each( function( cmp ) {
console.log( jQuery(this).text() );
} );
I wanted to extract all emails, but I had more than 1,000 rows and 17 columns/cells.
I used vanilla js, made some adjustments to get my desired output
var table = document.getElementsByTagName("table")[0]; //first table
var rows = table.rows;
//loop through rows
for (var i = 0; i < rows.length; i+=1) {
var emailTr = rows[i];
var emailTd = emailTr.cells[2]; //target third column/cell
var email = emailTd.innerHTML; //get the value
console.log(email + ', ');
var node = document.createElement("span"); // create span element
node.innerHTML = email + ', '; // desired output
document.body.appendChild(node); // display to document body
}
Assuming you're using vanilla Javascript (no libraries such as jQuery), and that this is the only table on the page, you can use the following code to select the third tr in the table, then find out what the td element contains
var table = document.getElementsByTagName("table")[0];
var emailTr = table.rows[2];
var emailTd = emailTr.cells[0];
var email = emailTd.innerHTML;
jQuery would make this easier
var email = $("table").children("tr:eq(2)").children("td").html();
A simple way is to give it a common class. Try:
<table>
<tr><td class="email">foo</td></tr>
<tr><td class="email">bar</td></tr>
<tr><td class="email">abc#yahoo.com</td></tr>
</table>
<script>
function getEmail(){
var email = new Array();
var arr = document.getElementsByClassName('email');
for(var i=0; i< arr.length; i++){
email.push(arr[i].innerHTML);
}
alert(email.join(','));
}
</script>
Demo
This is a solution in case you are using or plan to use jQuery library.
Given the email is always in the third row and first column (like in your example) then you can do as follows:
email = $('table tr:nth-child(3) td:first-child').html();
See working demo
Get all the <tr> elements. Loop through each one and compare the innerHTML against a regex that matches email addresses.
var emailAddresses = [];
var cells = document.getElementsByTagName("td");
for (var i = 0; i < cells.length; i++) {
if (cells[i].innerHTML.match(/yourEmailRegex/g)) {
emailAddresses[emailAddresses.length] = cells[i].innerHTML;
}
}
Find the appropriate regular expression here http://www.regular-expressions.info/email.html
in my case i want fifth column value of last row
var rows = document.getElementsByTagName("tbody")[0].rows;
var last = rows[rows.length - 1];
var cell = last.cells[4];
console.log(cell.textContent);