Javascript copy element value where element name has period

If the identifier of an html element does not have a period in it, then copying between elements is, of course, trivial, for example:

var theForm = document.paymentForm; theForm.BillStreet1.value = theForm.ShipStreet1.value; 

I have a case where I need to have a period in my identifiers, namely id = "bill.street1" and id = "ship.street1", and the following does not work: - (

  theForm.bill.street1.value = theForm.ship.street1.value; 

Could you let me know how to deal with this period? Is jquery easier?

+4
source share
2 answers

jQuery makes it easy to use css selectors to access elements. However, if you do not want to use jQuery, you can access the element this way, I suppose.

 theForm['bill.street1'].value = theForm['ship.street1'].value; 

I have not tested this, but it should work, because periods are an alternative method of accessing the array, iirc.

Be sure to use theForm['bill.street1'].value = theForm['ship.street1'].value; not theForm.['bill.street1'].value = theForm.['ship.street1'].value; . Additional periods make the format incorrect, in the same way using array.[2] instead of array[2] will invalidate it.

+4
source
 theForm['bill.street1'].value theForm['ship.street1'].value 
+3
source

All Articles