• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

Toggling href attribute with JavaScript

How can the code below be modified to reset the link location to allow multiple uses?
Kept getting nr = null, whenI tried to use function inputs (a & b) in lines 10 & 20 look like so:
link.setAttribute("href", "javascript:hide_row(\'a\',\'b\')");
link.setAttribute("href", "javascript:add_row(\'a\',\'b\')");

Open in new window


<script type="text/javascript">
function add_row (a,b) {
	// Make the row visible
	ar = document.getElementById(a);
	ar.style.display = \'table-row\';
	// Change the image to up.gif (from down.gif)
	document.image1.src = "/images/up.gif";
	// Swap the href to become hide_row & set it
	var link = document.getElementById(b);
	link.setAttribute("href", "javascript:hide_row(\'mod_subs\',\'href1\')");
}
function hide_row (a,b) {
	// Hide the row
	hr = document.getElementById(a);
	hr.style.display = \'none\';
	// Change the image to down.gif (from up.gif)
	document.image1.src = "/images/down.gif";
	// Swap the href to become add_row & set it
	var link = document.getElementById(b);
	link.setAttribute("href", "javascript:add_row(\'mod_subs\',\'href1\')");	
}
</script>

Open in new window

Or, if someone has a simpler way to do both in one function, please advise.
I have been attempting to increase my knowledge of javascript and this seemed like practice.
0
dolan2go
Asked:
dolan2go
  • 5
  • 3
1 Solution
 
BadotzCommented:
This:

link.setAttribute("href", "javascript:hide_row(\'mod_subs\',\'href1\')");

should be this:

link.setAttribute("href", "hide_row('mod_subs','href1')");

In your function, you never reference the parameters "a" or "b".
0
 
BadotzCommented:
Oh, sorry, you DO reference the parameters.

You should change the other stmt from this:

link.setAttribute("href", "javascript:add_row(\'mod_subs\',\'href1\')");

to this:

link.setAttribute("href", "add_row('mod_subs','href1')");
0
 
dolan2goAuthor Commented:
@Badotz,

Oops. My mistake. The slashes are for the php script.

So I'd like this:
link.setAttribute("href", "add_row('mod_subs','href1')");

Open in new window

to become this:
link.setAttribute("href", "add_row(a,b)");

Open in new window

But, the variables are not being passed on the 1st try, the add_row(a,b) function.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
BadotzCommented:
That's because you are passing the literal value for "a" and "b", NOT the value of the variables themselves.

Try this:
//
link.setAttribute("href", "add_row(" + a + "," + b + ")");
//

Open in new window

0
 
dolan2goAuthor Commented:
Thanks!

I was just looking at that and testing.

The console was outputting 'hr is null'.

Found the error. Lines 10 & 20 should have had the variables quoted like so:
link.setAttribute("href", "add_row('" + a + "','" + b + "')");

Open in new window

And it works!!
0
 
BadotzCommented:
Then my work here is done ;-)
0
 
dolan2goAuthor Commented:
"Then my work here is done ;-)"

Thank you for the quick response, repeated replies and helping me get on track. I would have graded higher if you'd have suggested the missing single quotes.
0
 
BadotzCommented:
Had I known the values were strings, I would have used apostrophes. Alas, you neglected to say...

But no worries, glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now