473,396 Members | 1,840 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,396 software developers and data experts.

JavaScript onclick event only partially working

I have a DataList which contains several LinkButtons, which are used to
select a category in my application. I want the currently selected category
to use a different CSS class. Here is an example of the generated code for
one of the buttons:

<a
onclick="UnselectCategories();datCategories_ctl00_ lnkCategory.className='Category_Selected';"
id="datCategories_ctl00_lnkCategory" class="Category_Selected"
onmouseover="categorystyle=this.className;this.cla ssName='Category_Selected';"
onmouseout="this.className=categorystyle;"
href="javascript:__doPostBack('datCategories$ctl00 $lnkCategory','')"
style="display:block;">Computers/Electronics</a>

The onmouseover and onmouseout events (which I use to create a rollover to
switch between the selected/unselected style classes) work perfectly fine.
The onclick event, however, only partially works. The function
"UnselectCategories();" (which changes all the buttons to unselected) works
no problem. However, the statement that assigns a value to the className
property does not appear to be working. I have tried using both of the
following for this statement:

//Using the literal element id:
datCategories_ctl00_lnkCategory.className='Categor y_Selected';

//Using the keyword this:
this.className='Category_Selected';

I would prefer to use the keyword this so that I do not have to
programmatically generate the code, but neither one seemed to work. I am
confused by this, for multiple reasons:

1. UnselectCategories() is being called, so why isn't the other statement?

2. Note that the statement this.className='Category_Selected'; (I know it is
not the one in my example, but I tried it using both the element id and the
keyword this) is no different than the second statement in the onmouseover
event. What is making them different? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Sep 7 '07 #1
6 2155
Lit
Have you tried

this.style.class='Category_Selected';

make sure the control has a class property to begin with.

Lit
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:Oi****************@TK2MSFTNGP02.phx.gbl...
>I have a DataList which contains several LinkButtons, which are used to
select a category in my application. I want the currently selected category
to use a different CSS class. Here is an example of the generated code for
one of the buttons:

<a
onclick="UnselectCategories();datCategories_ctl00_ lnkCategory.className='Category_Selected';"
id="datCategories_ctl00_lnkCategory" class="Category_Selected"
onmouseover="categorystyle=this.className;this.cla ssName='Category_Selected';"
onmouseout="this.className=categorystyle;"
href="javascript:__doPostBack('datCategories$ctl00 $lnkCategory','')"
style="display:block;">Computers/Electronics</a>

The onmouseover and onmouseout events (which I use to create a rollover to
switch between the selected/unselected style classes) work perfectly fine.
The onclick event, however, only partially works. The function
"UnselectCategories();" (which changes all the buttons to unselected)
works no problem. However, the statement that assigns a value to the
className property does not appear to be working. I have tried using both
of the following for this statement:

//Using the literal element id:
datCategories_ctl00_lnkCategory.className='Categor y_Selected';

//Using the keyword this:
this.className='Category_Selected';

I would prefer to use the keyword this so that I do not have to
programmatically generate the code, but neither one seemed to work. I am
confused by this, for multiple reasons:

1. UnselectCategories() is being called, so why isn't the other statement?

2. Note that the statement this.className='Category_Selected'; (I know it
is not the one in my example, but I tried it using both the element id and
the keyword this) is no different than the second statement in the
onmouseover event. What is making them different? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Sep 7 '07 #2
Lit said the following on 9/7/2007 6:12 PM:
Have you tried

this.style.class='Category_Selected';

make sure the control has a class property to begin with.
You can try, but this.style.className might work better.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 7 '07 #3
Lit
Both work

Lit

"Randy Webb" <Hi************@aol.comwrote in message
news:e9********************@giganews.com...
Lit said the following on 9/7/2007 6:12 PM:
>Have you tried

this.style.class='Category_Selected';

make sure the control has a class property to begin with.

You can try, but this.style.className might work better.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/

Sep 7 '07 #4
I have tried using this.style.className, but I am pretty sure that is
unrelated to the problem, because if that were the problem, my onmouseover
and onmouseout events would not work, and neither would my
UnselectCategories() function. Even though I doubt that it has anything to
do with the problem, here is the script section of my code that contains the
UnselectCategories() function:

<script type="text/javascript">
<!--
function UnselectCategories()
{
datCategories_ctl00_lnkCategory.className='Categor y_Unselected';
datCategories_ctl01_lnkCategory.className='Categor y_Unselected';
datCategories_ctl02_lnkCategory.className='Categor y_Unselected';
datCategories_ctl03_lnkCategory.className='Categor y_Unselected';
}
// -->
</script>

To see how and where I call this function and attempt to use the .className
property, see my original posting. Any help would be greatly appreciated.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Lit" <sq**********@hotmail.comwrote in message
news:Ol****************@TK2MSFTNGP06.phx.gbl...
Both work

Lit

"Randy Webb" <Hi************@aol.comwrote in message
news:e9********************@giganews.com...
>Lit said the following on 9/7/2007 6:12 PM:
>>Have you tried

this.style.class='Category_Selected';

make sure the control has a class property to begin with.

You can try, but this.style.className might work better.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/


Sep 7 '07 #5
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I tried that as well, but it did not make any difference. Is it possible
that the fact the onclick event and the action caused by having an href
are
"blending"? (is it possible that href is being executed before the class
is
set?) I am also using AJAX, and something else that I am wondering is if
it
is possible that the CSS class is being changed there when the results are
returned (I do not rebind the DataList or set the CssClass attribute, or
even update the UpdatePanel, although in the DataList's ItemTemplate in
the
*.ASPX file, the LinkButton's CssClass attribute is set to
"Category_Unselected") However, I doubt that the AJAX is the problem,
because before I added an onclick event the initially selected item (I
used
the If Not IsPostBack() condition in ASP.NET's Load event) remained
selected, but now it becomes unselected because of my UnselectCategories()
function. Any ideas? Thanks.
--
Getting clarity on the actual problem is difficult. Not only do you have
your own JScript code but you also have AJAX code which we can't see clearly
also ASP.NET code which again we can't see.

Here is a simple statement of fact:-

this.style.className = "Category_Selected"

when included in the onclick will set the class of the clicked element
correctly. If placed after your UnselectCategories it will work.

However, if you were also to correct onmouseover and onmouseout you will
find the the class is reverted to unselected on mouse out since the
categorystyle variable will hold the class read during onmouseover.

Having said that it could be that the postback in the HREF is also modifying
the HTML.
--
Anthony Jones - MVP ASP/ASP.NET
Sep 9 '07 #6
"Lit" wrote:
Have you tried

this.style.class='Category_Selected';

make sure the control has a class property to begin with.
There is nothing prohibiting that assignment, but the DOM specifies
className as a property of the HTMLElement, not of the HTMLStyleElement.
[this.className] is correct.

http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-95362176

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.

Sep 12 '07 #7

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

Similar topics

14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
9
by: tshad | last post by:
This is from my previous post, but a different issue. I have the following Javascript routine that opens a popup page, but doesn't seem to work if called from an asp.net button. It seems to work...
12
by: tshad | last post by:
I have the following code that attaches a Javascript confirm box to my checkbox. When I select the checkbox, the window comes up fine, but it never executes the XfertoDefault_Click function when I...
4
by: Mark Miller | last post by:
I've been trying to execute a javascript function just before submit on a form that contains an <input type="file"> input field and it isn't working. The reason I want to do this is the end users...
4
by: Peter Bons | last post by:
Hi all, I have an asp.net page with a button that has a javascript onclick method attached to it to display a modal dialog like this: If reportHasParameters Then 'add client side onclick()...
26
by: wardy | last post by:
Hi all, looking for a little bit of help.....I'm currently in the process of trying to understand the impact of the 508 guidelines on a web site that I am involved with, and I have a question...
4
by: jnag | last post by:
Hello, I have date fields on a form which is generated by a Coldfusion custom tag (another piece of code). I want to change something else on the form when the user changes the date. But, how do...
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:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.