Hi All,

I am having a problem changing HREF and Text of a link from javascript. My code is as colos

<a href="javascript:Action(9,'remove');" id="thelink">Remove</a>

And the javascript code is:

Code:

function Action(locid,reqact){
var fltitle;
var flllink;
var flmsg;

	   if(reqact=="remove"){
			   atitle="Add";
			   newlink="Action(" + locid + ",'add')";
		}
		   
		 else if(reqact=="cancel"){//cancel sent
		   atitle="Add";
		   newlink="Action(" + locid + ",'add')";
		  
		   }
 else if(reqact=="add"){//add sent
		   atitle="Add";
		   newlink="Action(" + locid + ",'cancel')";
		  
		   }
		   
		   
		  
var al=document.getElementById("thelink");
al.innerHTML=atitle;
al.href="javascript:"+newlink;
//al.setAttribute('href',newlink);

}//end function

At first page load, it works fine. But then the link text nor its href don't change.

What could be the error please?

Recommended Answers

All 2 Replies

If you look at the code properly you will notice that at the following block of code,

else if(reqact=="add"){//add sent
		   atitle="Add";
		   newlink="Action(" + locid + ",'cancel')";
 
		   }

The value of atitle is set to 'Add' instead of 'Cancel'. Hence you fell that it is not working :-)

Hmm... I think it is not that... It should be that...

newlink="Action(" + locid + ",'add')";

Here, you are passing 'locid' as a string to the function, but instead you are passing it as a variable. You need to do it as followed:

newlink="Action('" + locid + "','add')";

You are not supposed to use your variable as a string outside the function. You need to be more specific on that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.