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>
elements, but then you useelement.setAttribute. Do you see the difference?required pattern}of the function definition.required: "required",