Jump to content

Sometimes it's hard to see the forest for the trees.


Chris Bennett
 Share

Recommended Posts

Reminder to myself to occasionally step back and think about what I am trying to do.

Background:

Working on an autocomplete function using google places.
Allows clients or me to fill out boring address block and accurately geocode all at once from single input.
No repetition, no fuss, for single business or multiple branches using repeaters.

Not at all necessary, but figured why not, feels kinda nice and will not be costing me anything tucked away in the backend.

"Problem" and ridiculously easy solution:
Was working nicely, except when using enter/return to choose location, as save was getting fired on original input, throwing off the auto-filling.

Stupidly, I spent too much time trying to chase down php and api solutions, using many hooks without success attempting to prevent the page and field from saving.

Finally, feeling like a dumbarse, I took a step back and cursed my silliness.
Yep. If the thing you want to work around is javascript, maybe try javascript to work around it.

In the end, much to my embarrassment, the solution I sought was adding a few simple lines of js.
Define the form, and preventDefault on the form's submit once.

I cringe at how I missed the very simple solution while looking for the complicated workaround.

Won't be the last mistake I make, by a long shot, but hopefully the last time I make this particular one :)

window.addEventListener('focusin', function (event) {
	const input = event.target;
		
	if (input.classList.contains('autoComplete')) {

      	// Below, the 4 very simple lines I should have thought of much, much sooner
		
      	const form = document.getElementById('ProcessPageEdit');
		form.addEventListener('submit', function(e) {
  			e.preventDefault();
		}, { once: true });
          


 

  • Like 1
Link to comment
Share on other sites

Nice one! Will keep that in mind for future as well. 
 

if($inputfield.hasClass('InputfieldIgnoreChanges')) return false;

That said, it wouldn't have quite fitted my needs as I wanted it to be saved after the first ignore, so I would have had to remove the class anyway.
Would definitely come in handy elsewhere.

Link to comment
Share on other sites

 Share

×
×
  • Create New...