InvalidValueError: not an instance of HTMLInputElement

前端 未结 8 1639
刺人心
刺人心 2020-12-09 08:23

FYI, I\'m not a JavaScript Ninja but felt like I\'ll become one when I did a lot of things based on Google Maps recently.

I implemented a map. User can search for go

相关标签:
8条回答
  • 2020-12-09 09:16

    Your field is TEXTAREA, and from last updates, google maps autocomplete now supports only window.HTMLInputElement (INPUT tag).

    0 讨论(0)
  • 2020-12-09 09:19

    I was facing the same issue some days ago, but I've found a solution, if someone is interested :) It's not perfect, it's very tricky, but it does 95% of the job ! I know it's a very old topic, but if it can save somebody...

    The idea is to add a custom textarea, and bind the original input text value to the textarea (with keyup, keypress, keydown, change events).

    $('.MyInput').on('keyup keydown keypress change', function() {
      $('myTextarea').val($(this).val());
      emptyPlaceholder($(this), 'Myplaceholder text');
    });
    
    function emptyPlaceholder(input, placeholder) {
         // The placeholder of the textarea will hide if the input has a value
         if (input.val().length) {
             $('#triggerInput').attr('placeholder', '');
         } else {
             $('#triggerInput').attr('placeholder', placeholder);
         }
     }
    

    Add some css to " hide " the original input. It will hide what you're writing in the input, then you'll only see what is written in your textarea (Important: the z-index of the input has to be superior than textarea one !)

    .MyInput {
       color: transparent;
       background-color: transparent; 
    }
    

    It's far from being perfect, and I'm open if someone has a better solution !

    0 讨论(0)
提交回复
热议问题