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

How to set the Name of a button by JavaScript

How can I set the Name of a button?
I tried
xelement = document.createElement("input")
xelement.type = "button"
xelement.name = "MyButton"
but it does not work with the Internet Explorer (Mozilla is fine). I don't
get an error message but if I try to address this button by its name
e.g. document.getElementsByName("MyButton")[0].style.visibility = "hidden"
I get with the Internet Explorer (Mozilla works fine) the following error
message:
Error: 'document.getElementsByName(...).0.style' is null or not an object
Code: 0
Oct 31 '05 #1
7 5262

Stefan Mueller wrote:
How can I set the Name of a button?
I tried
xelement = document.createElement("input")
xelement.type = "button"
xelement.name = "MyButton"
but it does not work with the Internet Explorer (Mozilla is fine). I don't
get an error message but if I try to address this button by its name
e.g. document.getElementsByName("MyButton")[0].style.visibility = "hidden"
I get with the Internet Explorer (Mozilla works fine) the following error
message:
Error: 'document.getElementsByName(...).0.style' is null or not an object
Code: 0


Don't know if this helps, but take a look at this:-

http://msdn.microsoft.com/library/de...entsbyname.asp

Quote from that page:

"Microsoft JScript allows the name to be changed at run time. This does
not cause the name in the programming model to change in the collection
of elements, but it does change the name used for submitting elements.

The NAME attribute cannot be set at run time on elements dynamically
created with the createElement method. To create an element with a name
attribute, include the attribute and value when using the createElement
method.

var oAnchor = document.createElement("<A NAME='AnchorName'></A>");"

Julian

Oct 31 '05 #2


Stefan Mueller wrote:

I tried
xelement = document.createElement("input")
xelement.type = "button"
xelement.name = "MyButton"
but it does not work with the Internet Explorer . I don't
get an error message but if I try to address this button by its name
e.g. document.getElementsByName("MyButton")[0].style.visibility = "hidden"
I get with the Internet Explorer (Mozilla works fine) the following error
message:
Error: 'document.getElementsByName(...).0.style' is null or not an object


This is a known and nasty problem with IE which is documented on MSDN,
for some reasons their implementation is not able to properly set the
name on element objects created the standard way
document.createElement('tagname')
If it is a button it is usually easy to simply do
xelement.id = xelement.name = 'MyButton'
and then that way you can access the button with document.getElementById
the same way other elements can be accessed by its id. Only make sure
the id is unique in the document. Of course the id does not need to be
the same as the name.

If you really need to set the name to access the element by its name
then there are various workarounds, MSDN
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/name_2.asp>
suggests to use the non standard and IE specific
var input = document.createElement(
'<input type="radio" name="radioName">');
This works in IE/Win (I think IE/Mac does not like that) and creates the
element object with proper type and name but that argument with some
markup in the call to createElement will give you lots of problems in
other implementations as some implementations will throw an execption,
others will return null, so it is a pain then to write script to deal
with all implementations.

Doing
var dummyDiv = document.createElement('div');
dummyDiv.innerHTML = '<input type="radio" name="radioName">';
var input = dummyDiv.firstChild;
is sort of a hack too but as far as I am aware does not need browser
specific code forks as IE 5 and onwards, Mozilla 1.0 and later, Netscape
6 and later, Opera 7 and later support that in HTML documents.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 31 '05 #3
Thanks a lot
xelement.id = xelement.name = "MyButton"
works great.

Even
document.getElementsByName("MyButton")[0].style.visibility = "hidden"
is working. To my surprise I don't need to use document.getElementById.

In whatever way, I don't understand the command 'xelement.id = xelement.name
= "MyButton"' with two '=' but it works. I'm really happy.

Many thanks
Stefan
Oct 31 '05 #4


Stefan Mueller wrote:

In whatever way, I don't understand the command 'xelement.id = xelement.name
= "MyButton"' with two '=' but it works.


It is simply a shorthand for
xelement.name = 'MyButton';
xelement.id = 'MyButton';
An assignment is an expression that evaluates to the assigned value so
you can chain them the way I did.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 31 '05 #5
> It is simply a shorthand for
xelement.name = 'MyButton';
xelement.id = 'MyButton';
An assignment is an expression that evaluates to the assigned value so you
can chain them the way I did.


Thanks for the explanation.
But I thought
xelement.name = 'MyButton'
does not work.
Does it mean that
xelement.id = 'MyButton'
does the trick?

Stefan
Oct 31 '05 #6


Stefan Mueller wrote:

But I thought
xelement.name = 'MyButton'
does not work.
It does not quite do in IE/Win what it should do, when the form is
submitted the name=value pair will be submitted (meaning somehow IE
notices the name assigment) but finding the element by its name does not
work (meaning the DOM structures are not updated properly with the name
assigment). But of course it works in other browsers so I use and
suggested to use to set both the name and the id
Does it mean that
xelement.id = 'MyButton'
does the trick?


as setting the id in addition to the name then allows proper form
submission and finding the element in the DOM across browsers.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 31 '05 #7
Thanks a lot for the explanation.

Stefan
Nov 1 '05 #8

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

Similar topics

1
by: Veverita | last post by:
Hi there I'm hoping that someone can help me with a question I have about javascript syntax. I got an html page that uploads an image and some text field to a database. What I'd like to do...
2
by: Mark | last post by:
Hi - I have a dynamically created table, which has a number of forms - each named consecutively as 'adduserX' where X is a number generated from ASP. Within each form, I need to have a button...
3
by: Jon W | last post by:
I have a db table like list in a form. All the buttons have the same name. How do I know what index the button has in the button array onclick. <button name="shed" value="Edit2"...
1
by: Jenny | last post by:
Hi, Can I create an array of tags by assigning same name to these tags? For example, I have two <p> tags with the same name t1. But document.all.b.value=document.all.t.length does not...
8
by: drillbit_99 | last post by:
I have an HTML page of thumbnail images. Each image can begin a slideshow of the images on the page by clicking on the image. This opens another HTML page that begins the slideshow using large...
6
by: btknorr | last post by:
The following html and javascript combination fails to execute in Internet Explorer...does anyone know why? If you change the input's attribute "name" to anything other than "item" it works just...
7
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a...
6
by: Dick Watson | last post by:
I had a page that works when setup like this: === <form name="frmCalc" action=""> <script type="text/javascript"> function btnCalc_onclick(abc) { return "got here with " + abc; }
7
by: moksha | last post by:
Hi, I am new to javascript and i am facing a problem in coding. plz help me out. I am using javascript for dynamically creating a table row which contains text boxes and radio...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: 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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.