Simulate a double click on an html element [duplicate] - javascript

This question already has answers here:
How to programmatically fire a dblclick event defined with addEventListener?
(2 answers)
Closed 2 months ago.
Yo,
For the past weeks i try to simulate a double on my element but i can't find a solution. I know the click() method exist but i can't find a smilary method for my problem. Thanks for your help 🙂

You can trigger dblclick event by using dispatchEvent like this:
const event = new MouseEvent('dblclick');
document.getElementById('myId').dispatchEvent(event);
If you are using JQuery, The second way is:
$('#myId').dblclick()
var button = document.getElementById("myButton");
button.addEventListener("dblclick", function() {
alert("Works!");
});
const event = new MouseEvent('dblclick');
button.dispatchEvent(event);
<button id="myButton">Test</button>

Related

can't interact with jquery loaded button that uses jquery [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
JavaScript does not fire after appending [duplicate]
(3 answers)
Closed 7 years ago.
Working on a poll/voting system where people can click to see results before voting, and then click back again to see the options once more.
The problem is when loading in the results, the button I load to show the options again won't seem to work:
The code for them is basically the same (I've put an alert to test if it was even picking up the button, it is not):
$('.results_button').click(function() {
var poll_id = $(this).data('poll-id');
$('.poll_content').load('/includes/ajax/poll_results.php', {'poll_id':poll_id});
});
$('.back_vote_button').click(function() {
window.alert("Test");
//var poll_id = $(this).data('poll-id');
//$('.poll_content').load('/includes/ajax/poll_options.php', {'poll_id':poll_id});
});
The actual code to the back_vote_button is this for example:
<button name="pollresults" class="back_vote_button" data-poll-id="1">Back to voting</button>
Is there something I am missing about interacting with jquery loaded content?
Is your button being created after the doc is loaded? If doesn't exist at the time that the document is created, the listener is not bound. You can either bind a listener event or you can change the click to listen to an already created object, for a bad example try this:
$(document).on('click', '.back_vote_button', function(){
window.alert("Test");
});
this should work but it will listen every time you click on the body and then determine what you clicked on if this were the problem.

Is it possible to run X event by clicking Y in jQuery(without accessing X)? [duplicate]

This question already has answers here:
Clicking on one button to trigger click event on another
(3 answers)
Closed 7 years ago.
Guess there is two click event X and Y.
$('X').on('click',function(){
//some function here
});
and
$('Y').on('click',function(){
//this will run the X event
});
Here,At the same time I don't have access of trigger X . So I need to run that function/event by clicking Y trigger.
Is it possible somehow?
You are looking for trigger:
$('Y').on('click',function(){
$('X').trigger('click');
});
You can always use .trigger()
Execute all handlers and behaviors attached to the matched elements for the given event type.
$('Y').on('click',function(){
$('X').trigger('click')
});
However, you can simply bind same event handler to both elements then you don't need to use the above method.
$('X, Y').on('click',function(){
//Do something
});

Jquery listener for change of element id [duplicate]

This question already has answers here:
Firing event on DOM attribute change
(6 answers)
Closed 8 years ago.
I am trying to set up a listener to fire when an elements id changes, I don't know if this is possible but here is my attempt (that isn't working).
$(".appSelect").on('change', function() {
//fire change event
});
maybe something like :
$(".appSelect").attr("id").on('change', function() {
Is this possible in jquery?
Thanks!
It's not possible with a listener because no event occurs. You'd have to periodically check the ID with a setInterval function or similar. Better, tie into the function that changes the ID with a callback.

how to change onmouseover attribute programmatic? [duplicate]

This question already has answers here:
How do you override inline onclick event?
(4 answers)
Closed 8 years ago.
my html code is here.
mouse event functions writing like this
<li class="gnb1" onmouseover="fn1('param_01');" onmouseout="fn2('param_02','param_01');" > ...
I want change function and parameters programmatical as jquery or javascript.
$("li.gnb1").attr("onmouseover", "new_function_name()");
this code is not working. help me. show your move!
You can unbind the old handler and add a new one.
// To remove
$( "#foo").unbind( "click" );
// Then add new binding
$( "#foo").click(function () { });
Documentation for unbind is here: http://api.jquery.com/unbind/
[Edit as per comment]
If you have the ability to change the HTML, you could use jQuery to perform your bindings as well as your unbindings.
[Further edit]
You can however remove an inline event handler with removeAttr
$('#foo').removeAttr('onclick');

how to trigger onchange event of element with javascript in IE without jQuery [duplicate]

This question already has answers here:
OnChange not firing in IE
(7 answers)
Closed 8 years ago.
I need to trigger onchange event of an input with JS. Right now I'm using this:
document.getElementById("myElement").onchange();
but it seems that IE can not execute that. I know i can trigger it with jQuery, but in this project i cant use it. Is there any other ways to do this?
in IE use fireEvent
document.getElementById("myElement").fireEvent('onchange');
for IE9+ use dispatchEvent
if(document.fireEvent) {
document.getElementById("myElement").fireEvent('onchange');
} else {
var event = document.createEvent("HTMLEvents");
event.initEvent("change",true,false);
document.getElementById("myElement").dispatchEvent(event);
}

Categories