Using Javascript to access an html table

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
bowlesj
Forum Contributor
Posts: 179
Joined: Fri Jul 18, 2014 1:54 pm

Using Javascript to access an html table

Post by bowlesj »

Hi, I have php code below that is creating the html entry for the 2nd column of a table.

Code: Select all

echo("<td><a href='frmKaraokeUpdateSongGroup.php?RecKey=$fldKF_Song_LU&fldKF_Key=$fldKF_Key'>$fldKF_Group</td>\n");
I am trying to get at the $fldKF_Group using javascript. I know how to find the cell and row using the command below but I can't figure out the part at the end where I mark it in bold.
var GroupCurr = document.getElementById("Table_KaraokeSchedule").rows[RowCnt].cells[1].can't figure out what to put here


I know how to do it if there is no href in the table cell (using .innerhtml). I know how to do it if there is an input textbox in the table cell. I have some example code to find the value of the href. In the past I have used .innerhtml and used the left string or right string commands (as shown below) to get at the data but I prefer not to do that. Google searches with parts of the command are not finding anything.

Any help would be appreciated.

Thanks,
John

So this is my sneaky way around not having the proper solution which does get the value.

Code: Select all

function funcCallShellToBIAB(fldKF_Key,fldMM_Key,RowCnt) {
	var GroupCurr = document.getElementById("Table_KaraokeSchedule").rows[RowCnt].cells[1].innerHTML;
	GroupCurr = Right(GroupCurr, 5);
	GroupCurr = Left(GroupCurr, 1);
	var GroupNext = document.getElementById("Table_KaraokeSchedule").rows[RowCnt + 1].cells[1].innerHTML;
	GroupNext = Right(GroupNext, 5);
	GroupNext = Left(GroupNext, 1);
	var mytest = 0;
	var mytest2 = 0;
	//window.location='frmKaraoke_ShellToBIAB.php?RecKey=' + fldKF_Key + "&fldMM_Key=" + fldMM_Key + "&GroupCurr=" + GroupCurr + "&GroupNext=" + GroupNext;
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Using Javascript to access an html table

Post by pickle »

If I were you I'd add an id to the <a />. That way you can reference it with:

Code: Select all

document.querySelector('#id-of-tag').style.fontWeight = 'bold';
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
bowlesj
Forum Contributor
Posts: 179
Joined: Fri Jul 18, 2014 1:54 pm

Re: Using Javascript to access an html table

Post by bowlesj »

Thanks pickle.

Actually someone on an html forum taught me the solution below. However I would like to try your method to see if I can get it to work and I can learn something new. So I emailed myself the link to this forum thread as a reminder to eventually get around to trying it.

John

Code: Select all

function funcCallShellToBIAB(fldKF_Key,fldMM_Key,RowCnt) {
    var GroupCurr = document.getElementById("Table_KaraokeSchedule").rows[RowCnt].cells[1].getElementsByTagName('a')[0].innerHTML; //The a name is the anchor which is between <a and </a>
    //var GroupCurr = document.getElementById("Table_KaraokeSchedule").rows[RowCnt].cells[1].getElementsByTagName('a')[0].innerText; //This gives the same value but in double quotes as text
    var GroupNext = document.getElementById("Table_KaraokeSchedule").rows[RowCnt + 1].cells[1].getElementsByTagName('a')[0].innerHTML; //The a name is the anchor which is between <a and </a>
    //var GroupNext = document.getElementById("Table_KaraokeSchedule").rows[RowCnt + 1].cells[1].getElementsByTagName('a')[0].innerText; //This gives the same value but in double quotes as text
    window.location='frmKaraoke_ShellToBIAB.php?RecKey=' + fldKF_Key + "&fldMM_Key=" + fldMM_Key + "&GroupCurr=" + GroupCurr + "&GroupNext=" + GroupNext;
}
Post Reply