473,624 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

whether to use setElement or just set the attribute directly

I am curious if there is a benefit to set attributes directly, in my
javascript, or to use setAttribute.

For example, I have this:
var input = document.create ElementNS(xhtml NS, 'input');
input.setAttrib ute('width', '20em');

I could have just called input.width='20 em'

When is each better to use, or is there no difference between them?

Thank you for any help.

May 21 '06 #1
1 2921
James Black wrote:
I am curious if there is a benefit to set attributes directly, in my
javascript, or to use setAttribute.

For example, I have this:
var input = document.create ElementNS(xhtml NS, 'input');
input.setAttrib ute('width', '20em');

I could have just called input.width='20 em'

When is each better to use, or is there no difference between them?

Input doesn't have a width attribute, so this isn't a good example. The
first form should throw an exception while the second will add a
property 'width' to 'input'.

For attributes defined in the DOM, you are better off using the second
form. setAttribute doesn't work as expected in IE, which can cause all
kinds of problems (try setting the class of an element with setAttribute).

--
Ian Collins.
May 21 '06 #2

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

Similar topics

8
9984
by: Roy Smith | last post by:
For the past 6-8 months, I've spent most of my time writing C++ and a little bit of Java. Both of these languages support and encourage the use of private data and explicit accessor functions, i.e. instead of writing x = foo.bar, you write x = foo.getBar(). Now that I'm back to writing Python, I find myself in a quandry. Historically, I've always avoided accessor functions and just reached directly into objects to get the value of...
6
3402
by: Martin Plantec | last post by:
Hi again, If I may, I have another, slightly more complex, XSLT question. (I'm learning, but it's not as easy as I would have thought!) I would like a rule that says: for every element that has an attribute "webclass", rename the attribute as "class", keeping the same value for the attribute (and keeping the same element name). In other words, I would like to rename an attribute, whatever the element it comes in. I gather it is...
2
3952
by: Bill Cohagan | last post by:
In my app I'm validating an XML file against an XSD which contains several attribute default value specifications. I'm performing the validation via an xml document load() using a ValidatingXMLReader. After loading the document (and validating it) I assume that the resulting doc will (as appropriate) have elements with attributes set to their default values in those cases where there was nothing specified in the input XML file. However,...
5
24404
by: netcoder77 | last post by:
Has anyone tried this in VB .NET or via VBScript? Can it be done? How do we handle retrieving a binary data format (the photo) using ADSI or VB .NET? All my searching on the net yielded no useful results except for a half completed Perl script which claimed to be able to upload and download photos stored in AD as part of the User account.
21
39457
by: James Black | last post by:
I am curious if there is a benefit to set attributes directly, in my javascript, or to use setAttribute. For example, I have this: var input = document.createElementNS(xhtmlNS, 'input'); input.setAttribute('width', '20em'); I could have just called input.width='20em' When is each better to use, or is there no difference between them?
18
6756
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work, until I subclassed a class using this technique and from there on things started screwing up. I finally tracked it down to self.__class__.attribute! What was happening is that the child classes each over-rode the class attribute at their level,...
4
1905
by: Travis | last post by:
Is it considered good practice to call a mutator when inside the same class or modify the attribute directly? So if there's a public method SetName() would it be better from say ::Init() to call SetName("none") or just set name="none"?
0
8175
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
8625
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
8336
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,...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
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
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2610
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
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1487
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.