Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing a parent element

Status
Not open for further replies.

Bontebok

Programmer
Nov 18, 2002
63
US
Basic structure of my dhtml calendar date picker:

Code:
<form ...><input type=&quot;text&quot; name=&quot;date_input&quot;></form>
<div id=&quot;date_picker&quot;>
  <iframe id=&quot;date_picker_iframe&quot; src=&quot;datepicker.asp&quot; />
</div>

Functions like a standard date picker; click on an icon, calendar pops up, choose the date, date is filled in the input box, and the calendar is hidden.

The calendar is initialized by calling create_datepicker(idname, optionaltitle) from the body onLoad(). This then document.writes as many calendars as you want to create.

You can create a single calendar and use it in multiple places on the page, or you can use multiple calendars on the same page. The onClick on the icon passes the input object to place the date in.

datepicker.asp has an onClick event on the day of the month that calls a function to place a date back into an input box located on the page with the div.

Code:
function return_value(formvalue) {
  var datepicker;
	
  if (datepicker=layerexist(parent.document, name)) {
    datepicker.parentNode.inputobject.value=formvalue;
    if (document.layers)
      datepicker.parentNode.visibility = &quot;hide&quot;;
    else
      datepicker.parentNode.style.visibility = &quot;hidden&quot;;
}

layerexist() simply finds an element by id in the document passed and returns the reference to that element.

inputobject is a variable I created to store a reference back to the <input> element to place the results after the user clicks on the day in the calendar.

The inputobject is stored in the div element's node.

The complete expression for passing the value to the <input> element looks something like this:

parent.document.getElementById('date_picker_iframe').parentNode.inputobject.value=formvalue;

Explained:

Find the iframe element (not the iframe's document), find the parentNode (div) set the value property of the inputobject (reference to input element) to the value passed (some date value, 12/2/2003 for example.)

It works, but seems a bit like overkill, there must be an easier way to reference the parent element directly.

Any ideas?

Bontebok

 
Try the Javascript forum for Javascript questions: forum216
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top