Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-04-2013, 03:57 PM   PM User | #1
azrina
New Coder

 
Join Date: Jul 2010
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
azrina is an unknown quantity at this point
To change link dynamically using innerHTML

Hi,

Below is my code.
I am stuck in line shows below.

Code:
function createRows() //to create rows using innerHTML
{
	var tbody = document.getElementById("CONTENT"); //tbody = your table body
	tbody.innerHTML = ""; //empty table body
	for (i=1; i<=73; i++)  
	{
		tr = tbody.insertRow(-1); //append a row in table body
		td = tr.insertCell(-1); td.innerHTML = referenceNo(i); //ref. no
		td = tr.insertCell(-1); td.innerHTML = trackNo(i);  //track
		td= tr.insertCell(-1); td.innerHTML = ; //I am stuck here
	}
}
I would like to have function call there where it will receive a value to link it to pdf file and also display the title.

In HTML, I would be writing it like below. But, I want it to be dynamically set according to the value of i.

Code:
<a href="3-Management & Marketing\103.pdf" target="_blank">XXXX</a>
How to do this? Pls help.

Last edited by azrina; 03-06-2013 at 12:59 PM..
azrina is offline   Reply With Quote
Old 03-04-2013, 04:49 PM   PM User | #2
azrina
New Coder

 
Join Date: Jul 2010
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
azrina is an unknown quantity at this point
Pls look into the code below.
When run on browser, it cannot find the path.
What is wrong?

Code:
function createRows() //to create rows using innerHTML
{
	var tbody = document.getElementById("CONTENT"); //tbody = your table body
	tbody.innerHTML = ""; //empty table body
	for (i=1; i<=73; i++)  
	{
		tr = tbody.insertRow(-1); //append a row in table body
		td = tr.insertCell(-1); td.innerHTML = referenceNo(i); //ref. no
		td = tr.insertCell(-1); td.innerHTML = trackNo(i);  //track
		td = tr.insertCell(-1); td.innerHTML = getTitle(i); 
	}
}


/** ----------------------------------------- TO RETURN TITLE -------------------------------------------------*/
function getTitle(i)
{
	var title = "";
	switch(i)
	{
		case 1:
			title = "<a href=\"3-Management & Marketing\103.pdf\" target=\"_blank\">XXXXX</a>";
			break;
	}
	return title;
}
azrina is offline   Reply With Quote
Old 03-04-2013, 05:03 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Backslashes in strings must be doubled:
Code:
title = "<a href=\"3-Management & Marketing\\103.pdf\" target=\"_blank\">XXXXX</a>";
BUt *PROBABLY* that should not be a backslash, anyway. Probably should be forward slash. And keep it simple:
Code:
title = '<a href="3-Management & Marketing/103.pdf" target="_blank">XXXXX</a>';
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
azrina (03-06-2013)
Old 03-04-2013, 05:26 PM   PM User | #4
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Why not just store the filenames in an array?
Code:
var files = ["3-Management & Marketing/103.pdf", "3-Management & Marketing/xxx.pdf", "3-Management & Marketing/yyy.pdf", ...and so on...];
Then in the createRows function:
Code:
td.innerHTML = '<a href="' + files[i-1] + '" target="_blank">XXXXX</a>';
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Users who have thanked glenngv for this post:
azrina (03-06-2013)
Old 03-06-2013, 08:08 AM   PM User | #5
azrina
New Coder

 
Join Date: Jul 2010
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
azrina is an unknown quantity at this point
Thanks.
Both can be used.
But it is convenient for me not to use array.
azrina is offline   Reply With Quote
Old 03-06-2013, 08:26 AM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Why not do it properly and get rid of the innerHTML. That way you avoid all the issues of nested quotes.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 03-06-2013, 12:52 PM   PM User | #7
azrina
New Coder

 
Join Date: Jul 2010
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
azrina is an unknown quantity at this point
How? Can you explain further?
I used document.write before this.
But the code does not work.
And they suggested innerHTML.
What is the best solution for this actually?
azrina is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:02 PM.


Advertisement
Log in to turn off these ads.