473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to change style of a clicked-on hyperlink

hi,
i have a list of hyperlinks (server-side generated), that have a href
value as follows:
href="javascript:AsyncRequest('someurl.asmx?..',th is)"

since the url is loaded asynchronously, i need to change the style of
the link after it gets clicked to let them know which one they clicked
on, e.g. set a background color or something. i have had difficulty
getting access to the style of the sending element.

i tried the following code to no avail.
function AsyncRequest(url, sender)
{
sender.style = "background-color:#000;"; // doesn't work
event.srcElement.style = "background-color:#000;"; // doesn't work
....
}

the event.srcElement approach gives me 'object required' errors in IE6
and firefox. i should mention that it would be difficult to generate a
unique ID for each hyperlink.

thanks for any tips
tim

Jul 23 '05 #1
4 4559
Ivo
"Tim_Mac" wrote
sender.style = "background-color:#000;"; // doesn't work


Try this:
sender.style.backgroundColor = "#000";

The style object provides access to the usual style properties, but then
written in camelCase as shown. And color values don't need a semicolon
within the quotes.
hth
Ivo
Jul 23 '05 #2
hi Ivo,
thanks for the reply. when i try that, firefox tells me "style has no
properties" and IE6 tells me "style is null or not an object".

i'm using a referenced source file:
<script language="javascript" src="../AsyncWeb.js"></script>

and here is a sample A tag from my code:
<a
href='javascript:Record("../Select.asmx/EnterPreference?ID=29488&pref=Direct-Conflict",this)'
Direct Conflict</a>


i tried some debugging and when i did an alert(typeof sender), i get
'object window', i would have thought this should be hyperlink or
something?

thanks for any tips
tim

Jul 23 '05 #3
Ivo
"Tim_Mac" <ti*@mackey.ie> wrote
<a

href='javascript:Record("../Select.asmx/EnterPreference?ID=29488&pref=Direct
-Conflict",this)'
Direct Conflict</a>


i tried some debugging and when i did an alert(typeof sender), i get
'object window', i would have thought this should be hyperlink or
something?


Hm, try putting the function call in a proper onclick event handler rather
than using the javascript: pseudo-protocol. Like so:

<a href=""
onclick="return Record('../Select.asmx?etc.',this);">Direct Conflict</a>

and return false from the function to prevent the href being followed. Even
better, specify an url to a page for those without javascript in that href,
that is the official way. See the FAQ of this newsgroup:
<URL: http://jibbering.com/faq/#FAQ4_24 >

hth
Ivo


Jul 23 '05 #4
Genius!!
ivo that has made my day. thanks a million for your help. it works
perfectly.

for anyone else who is using asynchronous requests, i noticed a useful
trick to allow non-javascript clients to still use the links, without
having to duplicate the actual href in the onclick handler: just use
'this.href' in the javascript function. probably obvious to folks like
Ivo but i thought i'd post it here for future reference.

<a href='Select.asmx?ID=1234&' onclick='return
SendAsync(this.href,this)'>Whatever</a>

*********** javascript source ***********
var xmlhttp;

function SendAsync(url, sender)
{
// change the colour of the link to give some feedback to the user
sender.style.backgroundColor = "#000";
sender.style.color = "#FFF";

if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
xmlhttp.onreadystatechange=xmlhttpChange
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
if (xmlhttp)
{
xmlhttp.onreadystatechange=xmlhttpChange
xmlhttp.open("GET",url,true)
xmlhttp.send()
}
}
return false;
}

function xmlhttpChange()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
alert(xmlhttp.responseText);
}
else
{
alert("Problem retrieving XML data")
}
}
}

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: liglin | last post by:
The following script changes the color of text with the onmousover event. How can it be modified so it changes the text when the button is clicked? I'd want to avoid layers or CSS. Thanks,...
4
by: Andrea | last post by:
Hi everyone. I'm working on a navigation menu (in a frameset) that I want to set the "active" link to black (the link that corresponds to the page displayed in the right side of the frame) to...
2
by: deko | last post by:
I'm trying to change the font style of a link when that link is clicked. But the link (sometimes) includes a named anchor. So I need to test for a given named anchor and apply style changes if...
6
by: SPG | last post by:
Hi, I wrote a little bit of script that loads an image from a thumbnail when the thumbnail is clicked. It is not very clever, and I have found it only works in IE.. could someone have a look...
1
by: Yeah | last post by:
I have a multiple choice quiz where I would like to use CSS to change the color of the answers upon clicking them. I would like to present the right and wrong answers up front, rather than direct...
1
by: kwicher | last post by:
Hi! I hope I will be able to escribe my problem clear enough. So I have the following definition in CSS file. #links { ...... } #links a { ........ }
3
by: Robert | last post by:
Hi, Is it possible to dynamically change the style of a class instead of just an individual element? If so could you point me in the right direction? Robert
2
by: lghtslpr | last post by:
I have an imagemap with several hotspots, and I want a Google Maps-style "floating bubble" to appear when a hotspot is clicked. The bubble would contain some text, a picture, and a close button. I...
2
by: pamelafluente | last post by:
Hi I am back with a question. I have something like: <span id ="SomeID1" onclick = "Clicked(this.id)"<div class=c1> Something here </div></span> <span id ="SomeID2" onclick =...
1
by: SSG | last post by:
Dear All, I have 10 text boxes... first 5 text boxes e visible , rest of the 5 are hidden... once i clicked the button , the hidden boxes should be visble.. can anyone help me hpw to write...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.