3 Replies - 772 Views - Last Post: 13 August 2012 - 10:34 PM

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

get elements from specific form?

Posted 13 August 2012 - 12:06 PM

I am trying to get checkbox selected from 1 of the 2 forms on the same page. onclick passes the form, but the way I do it, getting elements is not form-specific. This will get wrong elements from the other form. How do I specify the form to get the elements?

function choose(f)
{
    selected =[];
    var c=f['ckbox[]'];
    for(var i=0;i<c.length;i++){
      if(c[i].checked)
      {
	  selected[selected.length]=document.getElementsByTagName("input")[i].getAttribute("id");
      }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: get elements from specific form?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: get elements from specific form?

Posted 13 August 2012 - 12:15 PM

You can use the document.forms[] collection to refer to specific forms and then by name if you like. So for instance if you had two forms, each containing a textbox called "name" you could use...

// Get the name element of the first form
var name1 = document.forms[0].name;


// Get the name element of the second form
var name2 = document.forms[1].name;



Look up this forms collection property of the document object for more information. :)
Was This Post Helpful? 0
  • +
  • -

#3 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: get elements from specific form?

Posted 13 August 2012 - 01:02 PM

there is nothing like this?

selected[selected.length]=document.f.elements("ckbox[]")[i].getAttribute("id");

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3510
  • View blog
  • Posts: 10,135
  • Joined: 08-June 10

Re: get elements from specific form?

Posted 13 August 2012 - 10:34 PM

no, the elements property of a form is not a function.

though you can do document.form_name["element_name"]

This post has been edited by Dormilich: 13 August 2012 - 10:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1