469,349 Members | 1,646 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,349 developers. It's quick & easy.

get element by long name

in my jsp, there is something like:

<input type=text name"plateDetail.name" value="xxxx" />
How can I get this element in javascript? the following code does not
work:

var name = document.getElementByName("plateDetail.name").valu e;

I think it is because there is a '.' in the name.

Any idea?

Thanks,

qq

Feb 8 '06 #1
2 1289
qu******@yahoo.com wrote:
in my jsp, there is something like:

<input type=text name"plateDetail.name" value="xxxx" /> ^^^
<input type=text name="plateDetail.name" value="xxxx" />

Is this really XHTML? If not, use correct HTML tags (i.e. close the tag
with '>' not '/>').

How can I get this element in javascript? the following code does not
work:

var name = document.getElementByName("plateDetail.name").valu e; ^^^

What you are looking for is the document.getElementsByName method, note
the 's'.

It returns a collection, which is a nodeList (kind of like an array
without all the goodies like split and join) that does not have a value
property (it does have a length property).

It's also not a good idea to have a variable called 'name', since that
is also a property of the object you are getting a reference too. Call
it nameText or similar.

If there is only one element with the name 'plateDetail.name' then use:

var nameText = document.getElementsByName("plateDetail.name")[0].value;
Incidentally, you will find the forms collection more widely supported
than getElementsByName, so if the input is in a form (wrapped for posting):

var nameText =
document.forms['fName'].elements['plateDetail.name'].value;
where 'fName' is the name of the form that the input is in.

I think it is because there is a '.' in the name.


A period is perfectly OK in the value of a name attribute, is is your
invalid markup teamed with scripting errors and incorrect use of methods
that has you stymied.

--
Rob
Feb 8 '06 #2
qu******@yahoo.com wrote on 08 feb 2006 in comp.lang.javascript:
in my jsp, there is something like:
What has jsp to do with it?
<input type=text name"plateDetail.name" value="xxxx" />
Do you mean HTML?

You're missing the "=":

<input type='text' name='plateDetail.name' value='xxxx' >


How can I get this element in javascript? the following code does not
work:

var name = document.getElementByName("plateDetail.name").valu e;
Must be getElementsByName, you're missing the "s":
I think it is because there is a '.' in the name.


Not so.
Try:

============================
<input type='text' name='plateDetail.name' value='xxxx'>

<script type="text/javascript">
var myName = document.getElementsByName("plateDetail.name")[0].value;
alert(myName)
</script>
=============================

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Wole Ogunremi | last post: by
6 posts views Thread by Michael Hill | last post: by
1 post views Thread by IsValidUN | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.