473,729 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

name reference

This code does not working in Mozilla. Works fine in IE.
--------------
<input type=text value=100 name=textbox>
<script>
alert(textbox);
</script>
--------------
This perhaps, because of Microsoft policy to globalize all tag names.

Is there any method for cross-browser fix without using getelementbyid?
Is there any method to pass values from outside of <script>, inside?
(something like "global" in php functions)
Javascript must be kept inside <script> tag
Mar 15 '06 #1
24 2512

Chameleon wrote:
This code does not working in Mozilla. Works fine in IE....
Is there any method for cross-browser fix without using getelementbyid?


You'll want to wrap the input in a form tag with a name, then access it
through the document object. Something like...

<form name=aform>
<input type=text value=100 name=textbox>
</form>
<script>
alert(document. aform.textbox);
</script>

Mar 15 '06 #2
Hi,

getElementById is really the preferred way, but for older browsers you
can use:

alert( document.forms[0].elements['textbox'].value );

but you need to wrap your input fields with <form></form>, and the code
above assumes you only have one <form></form> in the page.

There is a downside to having <form></form>, when user presses ENTER on
an input field (e.g. text box), the browser would try to submit the
form, so you'd need to handle this as well.

I won't discuss this here as I have no time, but have a look at
www.quirksmode.org, it has very good articles about cross-browsers
javascript event handling which will help in solving this problem
(e.g.: detect if ENTER key is pressed in an input field, and ignore it
so the browser doesn't try to submit the form).

I created a function for selecting input field that looks like (I don't
have it with me so this is just on top of my head):

function get_field_objec t(id)
{
// if browser supports getElementById
if( document.getEle mentById )
return document.getEle mentById(id);
else
return document.forms[0].elements[id];
}

then use it like:

<script>
var t = get_field_objec t("textbox");

// if object is valid
if( t )
alert( t.value );
else
alert('Couldnt find object.');
</script>

I hope that helps a bit.

Kind Regards,

Sid
http://www.onlinesid.com/blogs

Mar 16 '06 #3
Chameleon wrote :
This code does not working in Mozilla. Works fine in IE.
--------------
<input type=text value=100 name=textbox>
<script>
alert(textbox);
</script>
--------------
This perhaps, because of Microsoft policy to globalize all tag names.

Is there any method for cross-browser fix without using getelementbyid?
Is there any method to pass values from outside of <script>, inside?
(something like "global" in php functions)
Javascript must be kept inside <script> tag

Using Web Standards in Your Web Pages
Accessing Elements with the W3C DOM
http://www.mozilla.org/docs/web-deve...tml#dom_access

explains it all.

Ge'rard
--
remove blah to email me
Mar 16 '06 #4
Crazy Code Ninja wrote:
getElementById is really the preferred way
Not really. Then you need to generate a unique ID for each input field and
add bloat to your page.
alert( document.forms[0].elements['textbox'].value );
but you need to wrap your input fields with <form></form>
which you need to do anyway to create valid html.
, and the
code above assumes you only have one <form></form> in the page.


Which is a bad practice. Use form names instead of indexes.

For more info, see http://www.JavascriptToolbox.com/bestpractices/

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Mar 16 '06 #5
Matt Kruse said on 16/03/2006 11:07 AM AEST:
Crazy Code Ninja wrote: [...]
alert( document.forms[0].elements['textbox'].value );
but you need to wrap your input fields with <form></form>

which you need to do anyway to create valid html.


Not at all - there is no requirement for input elements to be inside
form elements. They can appear anywhere in the body that an inline
element can appear.
--
Rob
Mar 16 '06 #6
Right, I should have said its my preferred way from my experience.

Having said that, I still think it's not good if you have several input
fields with the same ID. If you have that many input fields in one page
that you have difficulty in not having unique IDs for each of them,
then its a sure sign that you should have split your page.

IDs should be unique, unlike class names. It will make maintenance
easier, server side scripting wise, CSS wise, as well as Javascript
wise.
, and the
code above assumes you only have one <form></form> in the page.
Which is a bad practice. Use form names instead of indexes.


I was just warning him about my (quick crude simple) example, that it
assumes he has only one form.

Matt Kruse wrote: Crazy Code Ninja wrote:
getElementById is really the preferred way


Not really. Then you need to generate a unique ID for each input field and
add bloat to your page.
alert( document.forms[0].elements['textbox'].value );
but you need to wrap your input fields with <form></form>


which you need to do anyway to create valid html.
, and the
code above assumes you only have one <form></form> in the page.


Which is a bad practice. Use form names instead of indexes.

For more info, see http://www.JavascriptToolbox.com/bestpractices/

--
Matt Kruse


Mar 16 '06 #7
Matt Kruse wrote :
Crazy Code Ninja wrote:
getElementById is really the preferred way
Not really. Then you need to generate a unique ID for each input field

This is exactly what the HTML 4.01 recommendation says, btw:
"Applicatio ns should use the id attribute to identify elements."
http://www.w3.org/TR/html401/interac...adef-name-FORM

and add bloat to your page.
alert( document.forms[0].elements['textbox'].value );
but you need to wrap your input fields with <form></form>
which you need to do anyway to create valid html.


Not at all, as pointed out Rob.
, and the
code above assumes you only have one <form></form> in the page.


Which is a bad practice. Use form names instead of indexes.

As I pointed to you before, name attribute has been formally deprecated
in XHTML 1.0.
"in XHTML 1.0, the name attribute of these elements (a, applet, form,
frame, iframe, img, and map) is formally deprecated"
http://www.w3.org/TR/2002/REC-xhtml1-20020801/#h-4.10
So, unless you're using lots of forms in a single page (why would you
need several forms in the same page is beyond my comprehension), using
indexes is not a big deal.
Matt, you propose *best* javascript practices that, in my humble
opinion, are not as portable as they can be.... at least, on this name
versus index issue. Best practices should always promote valid, safe,
sound practices which will work reliably across browser, across
different DTDs and across code context.

For more info, see http://www.JavascriptToolbox.com/bestpractices/


Gérard
--
remove blah to email me
Mar 16 '06 #8
> Crazy Code Ninja wrote:
getElementById is really the preferred way
Not really. Then you need to generate a unique ID for each input field This is exactly what the HTML 4.01 recommendation says, btw:
"Applicatio ns should use the id attribute to identify elements."
http://www.w3.org/TR/html401/interac...adef-name-FORM


I'd still put name tag for older browsers, this is just my preferrence,
for example:

<input type="text" id="text1" name="text1" />

ofcourse this is a good practice (IMO) if you generate your html code
using server side script, otherwise leave it out to avoid having
different id/name.

Regards,

Sid

Mar 16 '06 #9
Crazy Code Ninja said the following on 3/15/2006 11:06 PM:
Crazy Code Ninja wrote:
getElementById is really the preferred way

Not really. Then you need to generate a unique ID for each input field

This is exactly what the HTML 4.01 recommendation says, btw:
"Applicatio ns should use the id attribute to identify elements."
http://www.w3.org/TR/html401/interac...adef-name-FORM


I'd still put name tag for older browsers, this is just my preferrence,
for example:

<input type="text" id="text1" name="text1" />

ofcourse this is a good practice (IMO) if you generate your html code
using server side script, otherwise leave it out to avoid having
different id/name.


Then you need to re-read what has been deprecated. Its name attributes
on FORM's, not on the elements of forms.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 16 '06 #10

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

Similar topics

1
3422
by: mark.reichman | last post by:
I have text fields in my form with the same name. I can reference the value of these fields in IE 6.0 like with document.form.field.value. However, netscape 4.7 seems to croak. Why? What is the equivalent way to get the value in netscape 4.7.
4
13634
by: Carl | last post by:
When using 'name' in the form, it works, when using 'id' it doesn't. Any comments about this? By the way, is this a good method or is it better to use 'getElementById'? Carl <body> <form name="myform"> <input type button id="antw1" value="test"> </form>
16
34563
by: Robert Mark Bram | last post by:
Hi All! Is there a way to reference a window by name without doing something like this: open (, 'windowName'); The open method will open a blank window if there is no window with such a name. I am trying to organise a navigation structure between two windows with content from the same host.. I have been trying the following:
4
10929
by: Wim Roffal | last post by:
Is it possible to use javascript to change the name of a field in a form? Thanks, Wim
12
2468
by: CJ | last post by:
Why won't this work? I am passing the name of the form (I have two that use this validation script) but I keep getting an error. Error reads: "document.which_form.name is null or not an object" HTML----------- Form is ----> <form action="thanks.php" method="post" name="contact_form" id="contact_form"> Name -------> <input type="text" name="name" id="name" size="25"> Button sends code -----> <input type="button" value="Submit Form"
1
307
by: John E. | last post by:
How can I compile two projects with a circular reference while giving them a strong name? There is a project that we have that has two components that reference each other e.g. A<->B thus creating A.dll and B.dll. I have a project (e.g. R with an assembly name of Com.MyCompany.R.dll) that has to use this mess and access A. So, R->A. Well, Com.MyCompany.R.dll is going in the GAC. It is set for strong name (entry in the assembly...
6
1862
by: Ken Kast | last post by:
Here's my situation. I have an object function Obj () { this.foo = null; } a function function bar() {...} another function
6
22532
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was 'myDog' without having to pass it as a parameter. //////////////////////////////// function...
20
6996
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
5
8033
by: Anne | last post by:
Hello! Here is the statement in question: --STATEMENT A SELECT * FROM dbo.myTable WHERE colX in (SELECT colX FROM dbo.sourceTable) The problem with Statement A is that 'colX' does not exist in 'dbo.sourceTable'. It does, however, certainly exist in 'dbo.myTable'. Breaking the statement down, we have:
0
8921
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
8763
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
9148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8151
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
6722
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
6022
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();...
0
4528
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
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
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.