How to change a innerHTML with using the id in <a> tag?

Discussion in 'JavaScript' started by aayybb, Jul 10, 2008.

  1. #1
    <div id ="testing">
    <a id ="test1" href="www.xxx.com" onmouseover=greeting('test1')>Hello</a>
    <a id ="test2" href="www.yyy.com" onmouseover=greeting('test2')>Bye</a>
    </div>

    to change the innderHTML of the 2 links inside <div> I will have to

    have the following in the function I called for the event onmouseover.

    document.getElementById('test1').innerHTML = 'good morning';
    document.getElementById('test2).innerHTML = 'have a great day';

    Is there any way I can change them without having id in the <a>..</a> at all? I have a lot of links on this particular webpage.
     
    aayybb, Jul 10, 2008 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    What I understand is you need to do bulk changing in links' captions. I could not fuly follow because I cant see code of function greeting.


    Let me know if it is not what you want. Or post the exact scenario what needs be implemented.

    regards
     
    Vooler, Jul 10, 2008 IP
  3. aayybb

    aayybb Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes. that is what I want.

    function greeting(id)
    {

    document.getElementById(id).innerHTML = "something else";

    }

    But I have tons of links inside the <div id="testing'><div>
    It will be too much pain to add an id to each individual <a></a>
     
    aayybb, Jul 10, 2008 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    Well create an array, what exactly is scenario? We might change mechanism of populating div.

    regards
     
    Vooler, Jul 10, 2008 IP
  5. aayybb

    aayybb Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    How do I create the array? I thought about it but don't know how to refer the links inside the <div>.
    Basically I just need to change the text between<a href=...>text</a> when mouseover. But I have a lot of them and I just need to change some of them but still a lot.
     
    aayybb, Jul 10, 2008 IP
  6. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #6
    Try this method.

    <script> function ABC(id,txt) { id.innerHTML = txt; }</script>
    
    <a onmouseover="javascript:ABC(this,'Link 1 text changed')">Link1</a>
    <a onmouseover="javascript:ABC(this,'Link 2 text changed')">Link2</a>
    <a onmouseover="javascript:ABC(this,'Link 3 text changed')">Link3</a>
    HTML:

    I hope it helps.

    regards
     
    Vooler, Jul 10, 2008 IP
  7. aayybb

    aayybb Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank you so much. It did the trick I want.
    I never understand the "this" thing for it. Any place I can read more about how to use "this"?
    Again, thank you very much.
     
    aayybb, Jul 10, 2008 IP
  8. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #8
    At any event when you use the keyword 'this', it actually points to the current object.
    in simple words, 'this' means 'me', so whatever kind of object you are creating event handler for, 'this' will always poin to itself.

    Glad it helped.

    regars
     
    Vooler, Jul 10, 2008 IP
  9. aayybb

    aayybb Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thank you very much.
     
    aayybb, Jul 13, 2008 IP