-1

The jsp page is not recognizing my array aa[] in the line document.dd.aa[i].value.... to be specific dd is my form name....

<script type="text/javascript">
function chk()
{
 var errmsg = new String("");
 var aa = new Array("t1","t2");
 for(var i=0;i<=1;i++)
 {
 var ddd = document.dd.aa[i].value;
 if(ddd=="")
 {
 errmsg += "Empty field:" +"\n";
 }
 }
 alert(errmsg);
}
</script>

Thx in advance :DD...

Greg Kopff
16.7k13 gold badges57 silver badges80 bronze badges
asked Jun 5, 2012 at 5:42

2 Answers 2

4
var aa = new Array("t1","t2"); 

Above is not part of the form. So var ddd = document.dd.aa[i].value; is wrong.

You can directly access aa array. So do like this.

var ddd = aa[i];
Greg Kopff
16.7k13 gold badges57 silver badges80 bronze badges
answered Jun 5, 2012 at 5:46
Sign up to request clarification or add additional context in comments.

Comments

4

To access the array, it's simply:

var ddd = aa[i];

Also, it's better to create the array using literals:

var aa = ['t1','t2'];

If dd is a form, then document.dd returns the DOM form element named dd. To access the elements, you need to traverse the element using DOM traversal methods, like getElementById, getElementsByTagName and so on.

answered Jun 5, 2012 at 5:43

2 Comments

Just to add a little bit to the answer: It's simply because javascript variables are not bound to the DOM so you don't need to navigate to access them. You use the DOM to get data from page's elements, like for example, user input.
@RobertoLinares thanks for the add. I missed the part that dd was a form.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.