The title has changed, but only in generated code. This is how JS works - it tackles only with presentation. Try to use Webdeveloper toolbar for Firefox because it has the option to show generated code along with the original one. If you need any other way, think of using some server-side programming.
My observations:
Never mix JS with HTML. If you need to update your code, it'll be much easier - search for "unobtrusive JS" to get a better grasp on the issue. <a href="javascript:..."> or <a href="#" onclick="..."> is therefore a bad coding practise.
Do not use large JS packages like JQuery for simple tasks. They're good only if used with their full potential.
Do you really need to perform HTML manipulation on the fly and why? Is title tooltip that important to your code? Who should benefit from it: end users or admin? What about accessibility?
INFO on the first code block:
- If we don't know what link to use, we have to collect all of them
- and then test the collection against onclick event
- Suggestions:
- use a better event handler ;)
- collect links only from particular part of the page (main or navigation div) to improve speed
- CAVEATS:
- this may slow page rendering
- title attribute is hardcoded and same for all links
function changeTitle1(){
// find the array of links inside the document
var a = document.getElementsByTagName('a');
// test the array against onclick event
for (var i = 0, j = a.length; i
- INFO on the second code block:
- We know exactly what link to check
- Suggestion:
- use a better event handler ;)
- CAVEATS:
- this can only be used on a single element
- title attribute is hardcoded and same for all links
function changeTitle2(){
// find the link
var a = document.getElementById('action_link');
// onclick event
a.onclick = function() {
try{
// change title value
this.title = 'This unique click happened as well!';
// or use setAttribute() method like this
/* this.setAttribute('title', 'This unique click happened as well!'); */
// disable default link action
return false;
}
// clear memory leaks with try-finally block
finally{a = null;}
}
}
window.onload = function() {
changeTitle1();
//changeTitle2();
}
@spiro: Regarding your second question about links current state and changing their CSS... well, use CSS :)
Short version:
<style type="text/css" media="screen">
#home #current-home a,
#contact #current-contact a,
#about #current-about a
{/* declarations to style the current state */}
</style>
</head>
<body id="home"> <!-- OR body id="contact" OR body id="about" -->
<a href="#" title="Home" class="current-home">Home</a>
<a href="#" title="Contact" class="current-contact">Contact</a>
<a href="#" title="About us" class="current-about">About us</a>
Long version: http://www.456bereastreet.com/archive/200503/setting_the_current_menu_state_with_css/
(read the comments as well)
Note: is it necessary to display the link if you're already on the current page?