473,756 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="Unsele ctCategories(); datCategories_c tl00_lnkCategor y.className='Ca tegory_Selected ';"
id="datCategori es_ctl00_lnkCat egory" class="Category _Selected"
onmouseover="ca tegorystyle=thi s.className;thi s.className='Ca tegory_Selected ';"
onmouseout="thi s.className=cat egorystyle;"
href="javascrip t:__doPostBack( 'datCategories$ ctl00$lnkCatego ry','')"
style="display: block;">Compute rs/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
"UnselectCatego ries();" (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_c tl00_lnkCategor y.className='Ca tegory_Selected ';

//Using the keyword this:
this.className= 'Category_Selec ted';

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

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

2. Note that the statement this.className= 'Category_Selec ted'; (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********@hotm ail.com
http://www.nathansokalski.com/
Sep 7 '07 #1
6 2177
Lit
Have you tried

this.style.clas s='Category_Sel ected';

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

Lit
"Nathan Sokalski" <nj********@hot mail.comwrote in message
news:Oi******** ********@TK2MSF TNGP02.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="Unsele ctCategories(); datCategories_c tl00_lnkCategor y.className='Ca tegory_Selected ';"
id="datCategori es_ctl00_lnkCat egory" class="Category _Selected"
onmouseover="ca tegorystyle=thi s.className;thi s.className='Ca tegory_Selected ';"
onmouseout="thi s.className=cat egorystyle;"
href="javascrip t:__doPostBack( 'datCategories$ ctl00$lnkCatego ry','')"
style="display: block;">Compute rs/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
"UnselectCatego ries();" (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_c tl00_lnkCategor y.className='Ca tegory_Selected ';

//Using the keyword this:
this.className= 'Category_Selec ted';

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

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

2. Note that the statement this.className= 'Category_Selec ted'; (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********@hotm ail.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.clas s='Category_Sel ected';

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

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript 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******** ************@gi ganews.com...
Lit said the following on 9/7/2007 6:12 PM:
>Have you tried

this.style.cla ss='Category_Se lected';

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

You can try, but this.style.clas sName might work better.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript 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.clas sName, 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
UnselectCategor ies() 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
UnselectCategor ies() function:

<script type="text/javascript">
<!--
function UnselectCategor ies()
{
datCategories_c tl00_lnkCategor y.className='Ca tegory_Unselect ed';
datCategories_c tl01_lnkCategor y.className='Ca tegory_Unselect ed';
datCategories_c tl02_lnkCategor y.className='Ca tegory_Unselect ed';
datCategories_c tl03_lnkCategor y.className='Ca tegory_Unselect ed';
}
// -->
</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********@hotm ail.com
http://www.nathansokalski.com/

"Lit" <sq**********@h otmail.comwrote in message
news:Ol******** ********@TK2MSF TNGP06.phx.gbl. ..
Both work

Lit

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

this.style.cl ass='Category_S elected';

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

You can try, but this.style.clas sName might work better.

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


Sep 7 '07 #5
"Nathan Sokalski" <nj********@hot mail.comwrote in message
news:%2******** ********@TK2MSF TNGP03.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_Unsel ected") 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 UnselectCategor ies()
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.clas sName = "Category_Selec ted"

when included in the onclick will set the class of the clicked element
correctly. If placed after your UnselectCategor ies 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.clas s='Category_Sel ected';

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 HTMLStyleElemen t.
[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
5484
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 that works well as an html page. It brings up a modal popup window that I have been trying to work out for days now and this was the closest I have been able to come. I added a little asp.net code and an asp.net button and cannot get it to
136
9439
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
9
4922
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 fine from a link. The button does bring up the popup window, but when I press the links on the page, it doesn't return or close the window. ****************************************************************************
12
2134
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 press the Yes button. If I take off the Javascript event, the checkbox works fine. XferToDefault.Attributes.Add("onclick", "return confirm('Are you sure you want copy these entries?');") <asp:CheckBox ID="XferToDefault" AutoPostBack="true"...
4
2796
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 will be uploading files that are between 1 and 2 Meg. in size, and they are not too knowledgeable about computers. I want to disable the form buttons so they can't hit them after they hit the "upload" button. We're trying to prevent them from...
4
2283
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() event handler to display the popup dialog for entering parametervalues btnChangeParameters.Attributes.Add("onclick", String.Format("window.showModalDialog
26
11077
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 surrounding the use of Javascript. The site currently relies on Javascript for navigating from page to page - I cannot find anything in the 508 guidelines like the 6.3 WAI checkpoint for AA compliance: "Ensure that pages are usable when scripts,...
4
2123
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 I generate an onChange() event on this field (since I only have the call to the custom tag in this form)? Thanks in advance for any suggestions.
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.