tags:

views:

147

answers:

4
  1. How can I get an HtmlElement if the source code doesn't have the "id=''", but only has the "name=''". Document.GetElementById() doesn't work, and Document.All[""] doesn't work (obviously, because there is no ID.

  2. How can I simulate a form post?

Question 1 is more important because I found it harder to search. Question 2 I think i can get with more searching.

A: 

You get the element by GetElementByName also.

Atul Yadav
Actually `GetElementsByName` because the name attribute doesn't have to be unique. http://www.w3schools.com/jsref/met_doc_getelementsbyname.asp
Damovisa
Doesn't work. Really.
Rudi
A: 

1. You can use the document.getElementsByName. It returns an array with all the elements that has the specified name (as names doesn't have to be unique like id:s).

2. You can use the submit method of a form element to post it. The form can either be an existing form in the page, or be created on the fly when you need it.

You can also use the XmlHttpRequest object to make a post, if you want to take care of the response yourself instead of having it load as a document.

Guffa
document.getElementsById -> no such thing.
Rudi
@Rudi: Oops. I meant `getElementsByName` of course.
Guffa
+1  A: 

GetElementsByTagName could get you all of the tags that you are after, then you can look through the returned collection for the element you are after (if there is more then one).

This post shows how to submit the form element.

Glenn Condron
GetElementsByTagName doesn't seem to work.
Rudi
How did you try to use it?
Glenn Condron
A: 

There seems to be some confusion here, so I'll summarize:

On the document object, there are a number of methods you can use to locate an element:

  • document.getElementById('myid'); will return a single DOM element (if it exists) with an id attribute equal to myid.
  • document.getElementsByName('myname'); will return an array of DOM elements with a name attribute equal to myname.
  • document.getElementsByTagName('div'); will return an array of DOM elements with a matching tag - in this case, all divs in the document.

The case is important.

Does that clear things up?

This should work with all browsers provided you're not trying to get elements inside an iframe or something like that, but if you're having trouble with different browsers, I'd suggest using something like jQuery. It abstracts the browser differences from you so you can use the same syntax regardless of the browser being used.

Damovisa