/    Sign up×
Community /Pin to ProfileBookmark

I was looking at the code posted in another thread by herodote92 shown below

[code]
<html>
<head>
<script language=’javascript’>
function ChangeIt() {
document.getElementById(‘my_link’).href =’http://www.altavista.com’;
document.getElementById(‘my_image’).src =’image2.gif’;
}
</script>
</head>
<body>
<table>
<tr>
<td>
<a href=’http://www.google.com’ id=’my_link’>
<image src=’image1.gif’ id=’my_image’></a>
</td>
<td>
<input type=’button’ onclick=’ChangeIt();’ value=’Click here to change’>
</td>
</tr>
</table>
</body>
</html>
[code]

I have been several hours using my limited javascript knowledge trying to adapt this for text links instead of image.
I have given up for now… need to read my books more!!!:D
Would it be possible to select the text- tip[idx] and the link – link[idx] from an array?:confused:

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@ExuroJan 28.2005 — Try this out:
&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
var myLinks = new Array(4);
myLinks[0] = {site:"Google",url:"http://www.google.com/"};
myLinks[1] = {site:"Yahoo",url:"http://www.yahoo.com/"};
myLinks[2] = {site:"AltaVista",url:"http://www.altavista.com/"};
myLinks[3] = {site:"MSN Search",url:"http://search.msn.com/"};

var linkNum = 0;

function ChangeIt() {
var myLink = document.getElementById("my_link");
linkNum = (++linkNum &lt; myLinks.length) ? linkNum : 0;
myLink.href = myLinks[linkNum].url;
myLink.innerHTML = myLinks[linkNum].site;
}
//--&gt;
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.google.com" id="my_link"&gt;Google&lt;/a&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type="button" onclick="ChangeIt();" value="Click here to change"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@herodote92Jan 28.2005 — Nice, it works. Of course, it would be better to use a <select> list in this case, so that you can have a look at all possible choices at once. If you do so, you use an 'onchange' instead of a 'onclick', and you just pick the selected choice.
Copy linkTweet thisAlerts:
@ExuroJan 28.2005 — If you're using a select list, then why not simply have the browser redirect when the user hits the button? It seems pointless to have the user select the site from the select box [i]and[/i] have to click a link.

Also, if you used a select box, you could make a very simple server-side app in CGI/Perl, PHP, ASP, or ASP.NET (whichever you have on your server) that would allow the user to still be redirected to the proper site even if they don't have javascript enabled.
Copy linkTweet thisAlerts:
@zipperauthorJan 29.2005 — I'll leave you to argue amongst yourselves.

I got the answer I wanted which I adapted to fit my needs and the way I have used it makes perfect sense.
×

Success!

Help @zipper spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...