www.webdeveloper.com
Results 1 to 4 of 4

Thread: Unobtrusive javascript link update.

Hybrid View

  1. #1
    Join Date
    Jun 2009
    Posts
    23

    Unobtrusive javascript link update.

    Hi,
    I am trying to update the contact in this code using an external js file and targeting the "Contact me" node using the DOM: (see my js attempt below.):

    <div id="navMain">
    <ol>
    <li><a href="/">Hey Brian?</a>||</li>
    <li><a href="/travels">photography</a>&raquo;</li>
    <li><a href="/travels/china/">china</a>&raquo;</li>
    <li>2010</li>
    </ol>
    <ul>
    <li><a href="#">Contact me</a></li>
    <li><span>date</span>|</li>
    </ul>
    <p><a href="index.php">img</a></p>
    </div>

    ----------

    external js: (as you can see, I can't even get the alert to work, so I am having trouble traversing the DOM.) Eventually, what I'd like is the js file to setAttribute of the <a href> to my current email.

    // contact me email

    var getnavmain = document.getElementById("navMain").getElementsByTagName("ul");

    var emailtarget = getnavmain.getElementsByTagNames('li')[0].innerHTML;

    alert(emailtarget);

    ------------

    Can anyone get me started?

    Thanks so much.

    Brian

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    Wouldn't it be easier to just give the link an id and then access it directly to set the href value?

    Code:
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title></title>
            <script type="text/javascript">
            window.onload=function(){
                var myEmail = 'qwerty@somewhere.com';
                document.getElementById('lnkContactMe').href = 'mailto:'+myEmail;
            }
            </script>
        </head>
        <body>
            <ul>
                <li><a href="#" id="lnkContactMe">Contact me</a></li>
                <li><span>date</span>|</li>
            </ul>
        </body>
    </html>

  3. #3
    Join Date
    Jun 2009
    Posts
    23
    Hi,
    I don't want to give the contact link an id.
    Instead like to use the id from the div and then traverse to the element and setAttribute either onmouseover or onload.

    I don't have the correct syntax though in my first example. Can anyone help

    Thanks

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Code:
    var getnavmain = document.getElementById("navMain").getElementsByTagName("ul");
    var emailtarget = getnavmain[0].getElementsByTagNames('li')[0].innerHTML;
    Because the variable getnavmain is a collection as well.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles