[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Toggling href attribute with JavaScript

Posted on 2011-10-25
8
Medium Priority
?
186 Views
Last Modified: 2012-05-12
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
Comment
Question by:dolan2go
  • 5
  • 3
8 Comments
 
LVL 29

Expert Comment

by:Badotz
ID: 37028284
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
 
LVL 29

Expert Comment

by:Badotz
ID: 37028290
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
 
LVL 1

Author Comment

by:dolan2go
ID: 37028298
@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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 29

Expert Comment

by:Badotz
ID: 37028318
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
 
LVL 1

Author Comment

by:dolan2go
ID: 37028354
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
 
LVL 29

Accepted Solution

by:
Badotz earned 2000 total points
ID: 37028577
Then my work here is done ;-)
0
 
LVL 1

Author Closing Comment

by:dolan2go
ID: 37028808
"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
 
LVL 29

Expert Comment

by:Badotz
ID: 37029029
Had I known the values were strings, I would have used apostrophes. Alas, you neglected to say...

But no worries, glad to help.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question