Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 74145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:18:44+00:00 2026-05-10T20:18:44+00:00

How can I go through all external links in a div with javascript, adding

  • 0

How can I go through all external links in a div with javascript, adding (or appending) a class and alt-text?

I guess I need to fetch all objects inside the div element, then check if each object is a , and check if the href attributen starts with http(s):// (should then be an external link), then add content to the alt and class attribute (if they don’t exist create them, if they do exists; append the wanted values).

But, how do I do this in code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:18:45+00:00Added an answer on May 10, 2026 at 8:18 pm

    This one is tested:

    <style type='text/css'> .AddedClass {   background-color: #88FF99; } </style> <script type='text/javascript'> window.onload = function () {   var re = /^(https?:\/\/[^\/]+).*$/;   var currentHref = window.location.href.replace(re, '$1');   var reLocal = new RegExp('^' + currentHref.replace(/\./, '\\.'));    var linksDiv = document.getElementById('Links');   if (linksDiv == null) return;   var links = linksDiv.getElementsByTagName('a');   for (var i = 0; i < links.length; i++)   {     var href = links[i].href;     if (href == '' || reLocal.test(href) || !/^http/.test(href))       continue;     if (links[i].className != undefined)     {       links[i].className += ' AddedClass';     }     else     {       links[i].className = 'AddedClass';     }     if (links[i].title != undefined && links[i].title != '')     {       links[i].title += ' (outside link)';     }     else     {       links[i].title = 'Outside link';     }   } } </script>  <div id='Links'> <a name='_Links'></a> <a href='/foo.asp'>FOO</a> <a href='ftp://FTP.org/FILE.zip'>FILE</a> <a href='http://example.com/somewhere.html'>SomeWhere</a> <a href='http://example.com/somewhere2.html' class='Gah'>SomeWhere 2</a> <a href='http://example.com/somewhere3.html' title='It goes somewhere'>SomeWhere 3</a> <a href='https://another-example.com/elsewhere.php?foo=bar'>ElseWhere 1</a> <a href='https://another-example.com/elsewhere.php?foo=boz' class='Doh'>ElseWhere 2</a> <a href='https://another-example.com/elsewhere.php?foo=rad' title='It goes elsewhere'>ElseWhere 3</a> <a href='deep/below/bar.asp'>BAR</a> <a href='javascript:ShowHideElement('me');'>Show/Hide</a> </div> 

    If you are on an account on a shared server, like http://big-server.com/~UserName/, you might want to hard-code the URL to go beyond the top level. On the other hand, you might want to alter the RE if you want http://foo.my-server.com and http://bar.my-server.com marked as local.

    [UPDATE] Improved robustness after good remarks…
    I don’t highlight FTP or other protocols, they probably deserve a distinct routine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 63k
  • Answers 63k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer A high disk queue length does not mean you have… May 11, 2026 at 10:37 am
  • added an answer You may want to take a look at these articles:… May 11, 2026 at 10:37 am
  • added an answer Change this line: var lbl = document.getElementById('lblDisplayDate'); to: var lbl… May 11, 2026 at 10:37 am

Related Questions

How can I go through all external links in a div with javascript, adding
How can I go through each of the properties in my custom object? It
How can I go about generating a Friendly URL in C#? Currently I simple
How can I go about storing a vb.net user defined object in a sql
How can I go about hosting flash content inside a WPF form and still
How can I go about making my routes recognise an optional prefix parameter as
How can I go about with writing a shell extension to display online files
How can I get a PHP function go to a specific website when it
Does anyone know how or where I can go to quickly extract a list
How can I get programmatic access to the call stack?

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.