views:

4149

answers:

13

Hi All:
I have a html tag , in that initially I want tooltip name as mentioned in the "title" attribute. After I click on the link, I want different tooltip name. I have used the below code but it is not working. Can anyone please help me?

<html>
<head>
<script>

function tool(){
document.getElementsByName(link).title = "after click";
}

</script>
</head>
<body>
    <a href="javascript:tool()" name ="link" title="before click">click here</a>
</body>
</html>
A: 

Using jquery, you can write the following command :

$("a#link").attr("title","after click");
Vikram
He's not using jQuery though, he's using straight DOM manipulation. Why is anybody's guess.
Justin Poliey
I have used your code but it throwing error "Object expected". What should i do now?
Why complicate things by adding jquery into the mix? This is trivial dhtml.
Jason Kester
+1  A: 
document.getElementsByName("link").title = "after click";
PQW
You need only the first item in the collection. See @Waxwing's answer.
Cerebrus
+1  A: 

You can use setAttribute(attributeName, value) on the element that you want to change.

document.getElementById('link').setAttribute('title', 'after click');

This also works for custom attributes

Sean
This is showing error "Object doen't support this property or method"
getElementsByName returns a NodeList, you have to specify what element you are going to use, i.e. getElementsByName(link)[some number].setAttribute([...]).
cic
My mistake. I thought you were selecting by id. I have updated the code.
Sean
+8  A: 

Get rid of the BR tag inside your SCRIPT element, and rewrite the tool function to:

function tool(){
    document.getElementsByName("link")[0].title = "after click";
}

and it will work. However, it is not the optimal way. If you assign an id parameter to the field you can at least use document.getElementById(..), or even better, use a library like jQuery.

waxwing
Showing error 'document.getElementsByName("link").0' is null or not an object....
" .0 "? I think you mean [0].
Joel Coehoorn
please note how the 0 is used. getElementsByName("link")[0] is the correct way to access the element at position 0getElementsByName("link").0 will try to access a property that does not exist hence your exception
Sean
Never use getElementsByName when getElementById is what you want to do. As you're seeing, it just makes the problem worse.
Jason Kester
-1. levik's answer (http://stackoverflow.com/questions/897541/how-to-change-title-attribute-in-a-tag-using-javascript/898003#898003) is the better solution here. He can add the function to as many links as he wants and control the title text for each one.
Grant Wagner
That is fair. I only pointed out the errors in the code. It is true that there exists far more idiomatic ways to handle the situations.
waxwing
A: 

I would use http://jquery.com/ download it and add a script reference to it

Give your a tag an id such as myLink

Then using JQuery you can write

$(document).ready(function () {

    $("#myLink").click(function(){

        $("#myLink").attr("title","after click");

    });

});
Nick Allen - Tungle139
i cant add new library because few restriction in our firm. Please tell sumother other option other than JQuery!
A: 

As waxwing suggests, it's better to use getElementById()

<a id="aLink" href="http://www.bla.com" title="hello">link</a>
 <script>

function tool(){
document.getElementById('aLink').title = "after click";
}

</script>
A: 

Please help.. This problem in not solved yet!

Is this really an answer? ;)
cic
A: 

Two things are wrong with your supplied code:

  • As the name implies, documentent.getElementById looks things up by ID, not name. You'll need to supply an ID in that anchor tag. Change your link to <a href="javascript:tool()" id="link" name="link" title="before click">click here</a>

  • You're feeding documentent.getElementById the contents of a variable called "link", which presumably doesn't exist. You actually wanted to pass a literal string, which needs quotes. Change that call to document.getElementById("link").title = "after click";

Putting it all together, here's what your code would look like:

<html>
<head>
 <script>
  function tool(){
   document.getElementById("link").title = "after click";
  }
 </script>
</head>
<body>
    <a href="javascript:tool()" id="link" name="link" title="before click">
  click here
 </a>
</body>
</html>
Jason Kester
+1  A: 

Correcting to Jason code:

write document.getElementById("link").title insteadof document.getElementsByName("link").title.

I have tested it on IE7 and Firefox 3.

<html>
<head>
    <script>
            function tool(){
                    document.getElementById("link").title = "after click";
            }
    </script>
</head>
<body>
    <a href="javascript:tool()" id="link" title="before click">
            click here
    </a>
</body>
</html>

You can also explore this article for creating nice tooltip using javascript: http ://devirtu.com/2008/08/14/how-to-make-tooltip-with-javascript/

Green Techy
Thanks for that. I guess that makes 3 things wrong with the OP's code. I've corrected my entry.
Jason Kester
+10  A: 

I am assuming this is just a simplified example of what you want to do, since changing the link onclick can be done inline:

 <a href="#" onclick="this.title='after click'" title="before click">...</a>

Either way, the best way is to avoid doing any lookups altogether. Simply pass in the reference to your link as a parameter to your function:

<script>
  function tool(link) {
    link.title = 'after click';
  }
</script>
<a href="#" onclick="tool(this)" title="before click">...</a>

Naturally, as Grant suggests, you can pass the title value to the function as well, but then you're really better off just doing the inline version all the way and skipping the function.

levik
+1. I'd make the solution a bit more generic, with: function tool(link, text) { link.title = text; } and <a href="#" onclick="tool(this, 'after click')" title="before click">...</a>. But for a one-off, the answer as-is is fine.
Grant Wagner
A: 
<html>
<head>
<script>
window.onload = function () {
    window.document.getElementById("link").onclick = function () {
        this.title = "after click";
        return false;
    };
};
</script>
</head>
<body>
    <a href="http://www.example.com" id="link" title="before click">Click Here</a>
</body>
</html>
A: 

Hello people,

I am tackling 2 issues here:

  1. Title is changed when mouse over, but not in the code:
    ...
    < a class="a" href="#" name="home" title="Home">Home
    ...
    document.getElementsByName("home")[0].title = "Title changed"; // working

  2. How to change the class of the link:

    FROM: < a class="a" href="#" name="home" title="Home">Home
    TO: < a class="current" href="#" name="home" title="Home">Home
    document.getElementsByName("home")[0].class = "current"; // not working

    Thanks!

you need 'className' not class
matpol
A: 
Kreso
<pre><code>function changeTitle1(){// find the array of linksvar a = document.getElementsByTagName('a');// test the array against onclick eventfor (var i = 0, j = a.length; i < j; i++){try{// onclick eventa[i].onclick = function() {// change title valuethis.title = 'OMG, you clicked me!';// or use setAttribute() method like this/* this.setAttribute('title', 'OMG, you clicked me!'); */// disable default link actionreturn false;}}// clear memory leaks with try-finally blockfinally{a[i] = null;}} // end for loop}</code></pre>
Kreso