0

I have a dblclick listener that adds values into an html input field. I also have an on change function that is supposed to enable a submit button when the value in that html input field changes. However, sblclicking, and therefore adding a value into the input field, does not enable the submit button. The button does become enabled if I click in the input field and edit the value by hand, though.

Is there a work around so that the dblclick listener can also trigger the on change function?

document.addEventListener("dblclick", function(){
    document.getElementById("name").value = "Hello World!";
});

document.getElementById("name").onchange = function() {
    document.getElementById("send").disabled = false;
}
<input id="name" type="text">
<input type="submit" id="send" value="Submit" disabled>

2
  • note: you do have to click off of the input field to get the submit button to enable, if you are editing the input field by hand!! Commented Aug 16, 2018 at 17:50
  • It's not supposed to. developer.mozilla.org/en-US/docs/Web/Events/change Commented Aug 16, 2018 at 18:03

6 Answers 6

2

Changing input values dynamically needs explicit event generation.

var el = document.getElementById('name') el.value = 'something'; el.dispatchEvent(new Event('change'));

Jsbin link

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

Comments

1

You can create an event to dispatch on the element on document dblclick. Also use oninput instead of onchange like the following way:

document.addEventListener("dblclick", function(){
  document.getElementById("name").value = "Hello World!";

  var event = document.createEvent('Event');
  event.initEvent('input', true, true);
  document.getElementById("name").dispatchEvent(event);
});

document.getElementById("name").oninput = function() {
  document.getElementById("send").disabled = false;
}
<input id="name" type="text">
<input type="submit" id="send" value="Submit" disabled>

Comments

0

you can do like this ...

document.getElementById("name").onchange = function() {
    document.getElementById("send").disabled = false;
}

document.addEventListener("dblclick", function(){
    document.getElementById("name").value = "Hello World!";
    if ("createEvent" in document) {
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent("change", false, true);
        document.getElementById("name").dispatchEvent(evt);
    }
    else
        document.getElementById("name").fireEvent("onchange");
});
<input id="name" type="text">
<input type="submit" id="send" value="Submit" disabled>

Comments

0

Normally properties modified by javascript do not trigger html change events.

So what you can do is call the code that activates your button from the change handler and the doubleclick handler. If the code was more complex you could wrap it in a function and call it from both handlers.

document.addEventListener("dblclick", function(){
    document.getElementById("name").value = "Hello World!";
    document.getElementById("send").disabled = false;
});

document.getElementById("name").onchange = function() {
    document.getElementById("send").disabled = false;
}

Comments

0

Did you try adding the onchange method to your initial function?

For example:

     document.getElementById("name").onchange = function() {
    document.getElementById("send").disabled = false;
}

document.addEventListener("dblclick", function(){
    document.getElementById("name").value = "Hello World!";
    if ("createEvent" in document) {
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent("change", false, true);
        document.getElementById("name").dispatchEvent(evt);
    }
    else
        document.getElementById("name").fireEvent("onchange");
});

Comments

-1

Use oninput instead of change

document.addEventListener("dblclick", function() {
  document.getElementById("name").value = "Hello World!";
});
document.getElementById("name").oninput = function() {
  document.getElementById("send").disabled = false;
}


<input id="name" type="text">
<input type="submit" id="send" value="Submit" disabled>

DEMO here

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.