[selectIndex] not working in "onclick"

Results 1 to 2 of 2

Thread: [selectIndex] not working in "onclick"

  1. #1
    Join Date
    Dec 1969

    Default [selectIndex] not working in "onclick"

    When I try to click this button and send it to a javascript function, it complains about the "[selectIndex]" part. But when I use the exact same command on a "onClick" event on another form type, it works.<BR><BR>How do I get it to work with a button?<BR><BR>Thanks so much!! I appreciate your help VERY MUCH!<BR>Andrea :)<BR><BR>&#060;INPUT TYPe="button" NAME="CalculateNow" value="ok"<BR> onClick="pSaveNeighborhood (window.document.Form_1.SubTotalPrice.options[selectedIndex].text, window.document.Form_1.NumberOfPeople.value);" &#062; <BR>

  2. #2
    Join Date
    Dec 1969

    Default Because it doesn't exist as coded...

    selectedIndex is a *PROPERTY* or a &#060;SELECT&#062; object.<BR><BR>So you&#039;d have to do:<BR><BR>&#060;INPUT TYPE="button" NAME="CalculateNow" value="ok"<BR>onClick="pSaveNeighborhood (document.Form_1.SubTotalPrice.options[document.Form_1.SubTotalPrice.selectedIndex].text, document.Form_1.NumberOfPeople.value);" &#062; <BR><BR>[On the other hand, your use of "window." in all those is superfluous.]<BR><BR>And we can write that shorter (but really identically) via:<BR><BR>&#060;INPUT TYPE="button" NAME="CalculateNow" value="ok"<BR>onClick="pSaveNeighborhood(this.form .SubTotalPrice.options[this.form.SubTotalPrice.selectedIndex].text, <BR> this.form.NumberOfPeople.value);" &#062; <BR><BR>And you could improve readability by writing a function:<BR><BR>&#060;SCRIPT&#062;<BR>function selectValue(sel)<BR>{<BR> var opt = sel.options[sel.selectedIndex];<BR> return opt.value != "" ? opt.value : opt.text;<BR>}<BR>&#060;/SCRIPT&#062;<BR><BR>Which just incidentally will grab the .text from an &#060;OPTION&#062; if there isn&#039;t any .value for it. And then you code:<BR><BR>&#060;INPUT TYPE="button" NAME="CalculateNow" value="ok"<BR> onClick="pSaveNeighborhood( selectValue(this.form.SubTotalPrice), this.form.NumberOfPeople.value);" &#062; <BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts