Changing background color of text box input not working when empty

前端 未结 8 1947
半阙折子戏
半阙折子戏 2020-12-29 07:37

I am having a tough time with this javascript code to change the background color of a text input if the input is empty.

Here is the code:

function c         


        
相关标签:
8条回答
  • 2020-12-29 08:00

    You can style it using javascript and css. Add the style to css and using javascript add/remove style using classlist property. Here is a JSFiddle for it.

      <div class="div-image-text">
        <input class="input-image-url" type="text" placeholder="Add text" name="input-image">
        <input type="button" onclick="addRemoteImage(event);" value="Submit">
      </div>
      <div class="no-image-url-error" name="input-image-error">Textbox empty</div>
    
    addRemoteImage = function(event) {
      var textbox = document.querySelector("input[name='input-image']"),
        imageUrl = textbox.value,
        errorDiv = document.querySelector("div[name='input-image-error']");
      if (imageUrl == "") {
        errorDiv.style.display = "block";
        textbox.classList.add('text-error');
        setTimeout(function() {
          errorDiv.style.removeProperty('display');
          textbox.classList.remove('text-error');
        }, 3000);
      } else {
        textbox.classList.remove('text-error');
      }
    }
    
    0 讨论(0)
  • 2020-12-29 08:09

    on body tag's onLoad try setting it like

    document.getElementById("subEmail").style.backgroundColor = "yellow";
    

    and after that on change of that input field check if some value is there, or paint it yellow like

    function checkFilled() {
    var inputVal = document.getElementById("subEmail");
    if (inputVal.value == "") {
        inputVal.style.backgroundColor = "yellow";
                }
        }
    
    0 讨论(0)
提交回复
热议问题