Jump to content

correct array reference syntax


cesarcesar

Recommended Posts

I am using a js calendar script that populates a text field with a date. When clicking in the field the calendar shows then i choose a date, calendar closes, the date is now in the field. great it works fine.

 

example- *note, array use[]*

<input type="text" id="example[]" name="example[]" value="" onfocus="if(self.gfPop)gfPop.fPopCalendar(this);return false;">

 

I want to make this call from a button. when adding the code  to a href, the popup shows but nothing happens when selecting a date.  I have tried the following scripts will failure.

 

*html a href and img tag left out*

onclick="if(self.gfPop)gfPop.fPopCalendar(document.this_form.elements['example[]']);return false;"

onclick="if(self.gfPop)gfPop.fPopCalendar(document.this_form.example[]);return false;"

onclick="if(self.gfPop)gfPop.fPopCalendar(this);return false;"

Link to comment
https://forums.phpfreaks.com/topic/74254-correct-array-reference-syntax/
Share on other sites

The "this" in the first code block refers to the input element.

 

If you were to put the button right next to the input field, you could pass

 

this.previousSibling

 

as the element reference

 

http://developer.mozilla.org/en/docs/DOM:element.previousSibling

The problem is that you're trying to access the object which has ([]) in the name. This make it an array and hard to access using JS by calling the name.

 

Since the ID is only used in the front end (the name will be using in the backend script like PHP), you can change the ID to example_1 and using document.getElementById('example_1') on the onclick event.

 

This way, you are sure that the object is passed to the calendar.

 

You might also try the NoGray calendar at http://www.nogray.com/calendar.php

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.