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

Accessing Class

Not sure if this is entirely a CSS issue but I have defined the class below
in CSS.

If I am stretching the friendship then I apologise in advance.

I am trying to write a javascript script to check that all required fields
in a form are entered.

The idea was to give all required input fields a class of say "mandatory"
and then check for fileds with .className=="mandatory"

But ... the class does not seem to be accessible

In HTML I have for example ...

<input class="mandatory" name="fax" type="text" id="fax" />

To test in javascript I have set tempobj to point at the element then
......
alert (tempobj.name);
alert (tempobj.className);
......

the first alert displays "fax"
the second alert displays null.

Can anyone suggest what I'm doing wrong?

Cheers.
rockoyster
Jul 21 '05 #1
3 2168
rockoyster wrote:
Not sure if this is entirely a CSS issue but I have defined the class
below in CSS.
To be picky about the subject; classes are defined in (X)HTML. CSS has
selectors which include a way to say "If the element has such and such
class".
I am trying to write a javascript script to check that all required fields
in a form are entered.
news://comp.lang.javascript would be a better place to ask.
The idea was to give all required input fields a class of say "mandatory"
and then check for fileds with .className=="mandatory" <input class="mandatory" name="fax" type="text" id="fax" /> alert (tempobj.name);
alert (tempobj.className); the first alert displays "fax"
the second alert displays null.


With the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<title>Class &amp; JS</title>
<h1>Class &amp; JS</h1>
<div><input class="mandatory" name="fax" type="text" id="fax"></div>
<script type="text/javascript">
el = document.getElementById('fax');
window.alert(el.name);
window.alert(el.className);
</script>

It works fine, and the class name is displayed. This really is a JS question
and has nothing to do with style sheets. You should try
comp.lang.javascript. It would also be a good idea to post a URI to the
complete webpage. Code fragments are all very well for narrowing down where
you think the problem lies, but when you're wrong its not very helpful.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jul 21 '05 #2
On Sat, 29 Jan 2005 07:02:09 GMT, rockoyster <ro*********@hotmail.com>
wrote:

[snip]
If I am stretching the friendship then I apologise in advance.
I'm afraid it is. This has nothing to do with style sheets on the Web, but
Web scripting. It should have been posted to comp.lang.javascript.
[...] check for fileds with .className=="mandatory"
Such a test is incorrect. It might work for the moment, but it isn't very
forward-thinking: the class attribute is a space separated list. You
should check that the attribute *contains* the value. The safest way would
be with a regular expression, allowing you to assert that you won't be
performing substring matches unintentionally:

if(/(^|\s+)mandatory($|\s+)/.test(element.className)) {
/* 'element' is mandatory */
}
But ... the class does not seem to be accessible


Please post a link to an example and state with what user agent(s) you
experience this problem.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 21 '05 #3

"Michael Winter" <M.******@blueyonder.co.invalid> wrote
<<snip>>
Such a test is incorrect. It might work for the moment, but it isn't very
forward-thinking: the class attribute is a space separated list. You
should check that the attribute *contains* the value. The safest way would
be with a regular expression, allowing you to assert that you won't be
performing substring matches unintentionally:


Despite having posted to the wrong group I was pleased to receive some very
helpful feedback along with the mild (and thoroughly deserved) chastisement.

I am indebted to Michael who's insightful comments alerted me to how
perverse my proposed use of the class attribute was. Seemed like a good
idea at the time.

The problem went away once I decided to use a more rational and maintainable
solution _and_ I learnt how to correctly use the className property in
Javascript!

rockoyster

Jul 21 '05 #4

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

Similar topics

5
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
3
by: Vivek Sharma | last post by:
Hi, I have created a dropdownlist as a web user control. I am using its multiple instances on the webpage. How do I access the selectedValue of each instance? All the instances have different...
3
by: AdamM | last post by:
Hi all, When I run my VbScript, I get the error: "ActiveX component can't create object: 'getobject'. Error 800A01AD". Any ideas what I did wrong? Here's my VBScript: dim o set...
1
by: Eirik Brattbakk | last post by:
Hi I have some problems accessing a soap service made in c# using an ATL/MFC client over SSL. I have tried both CSoapMSXMLInetClient and CSoapWininetClient as template arguments with my stub...
8
by: dwok | last post by:
I have been wondering this for a while now. Suppose I have a class that contains some private member variables. How should I access the variables throughout the class? Should I use properties that...
4
by: Kaush | last post by:
Hi all, I am creating a webservice to accept SOAP messages, parse the message and send a SOAP response back to the client accessing my web service using WSE-2 in ASP.NET. I am creating a class...
5
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of...
6
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
2
by: Jurek Dabrowski | last post by:
hi all, I have a question in reference to accessing variables in another class maybe someone has dealt with before. I have some public variables declared in my main plug-in class...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.