0

I am trying to create a form that changes attributes for an input field depending on a checkbox being checked. I saw in another question that we can create an element and its attributes with a similar function, but when I adapt it to this form, it's not working, anybody can help me do this? I know it's easier using JQuery, but I need this done in pure JS.

<!DOCTYPE html>
<html>
<body>

<form role="search" method="get" action="">
<input type="checkbox" id="check-minor" name="check-minor" onclick="searchFunc()"> <label for="check-minor">Menor</label>
<input type="text" name="qt" id="qt" placeholder="Write your Shop Code" required pattern="[0-9A-Za-z]{7,9})" size="30">
</div>
<button type="submit" class="btn btn-default" id="submit-button">Search</button>
</form>

<script>

function setAttributes(elements, attributes) {
  Object.keys(attributes).forEach(function(name) {
    element.setAttribute(name, attributes[name]);
  })
}

var qtpie = document.getElementById("qt");
function searchFunc() {
    if (document.getElementById("check-minor").checked){
        setAttributes(qtpie, {
            required pattern: "[0-9]{11}",
            placeholder: "Shop Code for Minors"
        }) 
    } else {
        document.getElementById("qt").placeholder= "Write your Shop Code"
    }

</script>

</body>
</html> 
6
  • 1
    The parameter is elements, but then you use element.setAttribute. Do you see the difference? Commented Aug 9, 2018 at 11:33
  • You got a space in your javascript object too required pattern Commented Aug 9, 2018 at 11:34
  • 1
    You're missing the closing } of the function definition. Commented Aug 9, 2018 at 11:35
  • 1
    It should be required: "required", Commented Aug 9, 2018 at 11:36
  • Aren't you seeing the error messages in the console? Commented Aug 9, 2018 at 11:36

1 Answer 1

1

Here you go you have some typo issue in your code and missing curly brackets

  function setAttributes(elements, attributes) {
      Object.keys(attributes).forEach(function (name) {
        elements.setAttribute(name, attributes[name]);
      })
    }

    var qtpie = document.getElementById("qt");

    function searchFunc() {
      if (document.getElementById("check-minor").checked) {
        setAttributes(qtpie, {
          required: "",
          pattern: "[0-9]{11}",
          placeholder: "Shop Code for Minors"
        })
      } else {
        document.getElementById("qt").placeholder = "Write your Shop Code"
      }
    }
<!DOCTYPE html>
<html>

<body>

  <form role="search" method="get" action="">
    <input type="checkbox" id="check-minor" name="check-minor" onclick="searchFunc()">
    <label for="check-minor">Menor</label>
    <input type="text" name="qt" id="qt" placeholder="Write your Shop Code" required pattern="[0-9A-Za-z]{7,9})" size="30">

    <button type="submit" class="btn btn-default" id="submit-button">Search</button>
  </form>

 

</body>

</html>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.