javascript - Trigger mouse-up programmatically - Stack Overflow

I would like to trigger a mouseup event programmatically given:<div id="abc">Click me&l

I would like to trigger a mouseup event programmatically given:

<div id="abc">Click me</div>

<script>
    document.getElementById('abc').addEventListener('mouseup', function () {
        alert('mouseup');
    });
</script>

Now, to trigger I've tried the following:

document.getElementById('abc').onmouseup();

Or with jQuery

$('#x').trigger('mouseup');

None of these result in an alert so I must be doing something wrong here.

DEMO

UPDATE: Fixed type with addEventListener

I would like to trigger a mouseup event programmatically given:

<div id="abc">Click me</div>

<script>
    document.getElementById('abc').addEventListener('mouseup', function () {
        alert('mouseup');
    });
</script>

Now, to trigger I've tried the following:

document.getElementById('abc').onmouseup();

Or with jQuery

$('#x').trigger('mouseup');

None of these result in an alert so I must be doing something wrong here.

DEMO

UPDATE: Fixed type with addEventListener

Share Improve this question edited Mar 7, 2020 at 16:47 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 9, 2016 at 19:04 Jeanluca ScaljeriJeanluca Scaljeri 29.2k66 gold badges235 silver badges382 bronze badges 2
  • 1 document.getElementById('abc').addEventListener('mouseup', function () { alert('mouseup'); }); It seems you forgot to add the id param in the getElementById func. Check here – Arsalan Commented Oct 9, 2016 at 19:09
  • in the code you pasted the id of the element you want to target is missing, also on your demo you call an ' onmouseup ' method without passing any callback function, if you have a look at the console you can see an error, although the code in your demo works fine without that line of code – jrod Commented Oct 9, 2016 at 19:27
Add a ment  | 

2 Answers 2

Reset to default 3

getElementById doesn't have a call and an argument in the code below.

document.getElementById.addEventListener('mouseup', function () {
    alert('mouseup');
});

right example

document.getElementById("the id of the element").addEventListener('mouseup', function () {
    alert('mouseup');
});

and if you want to trigger the event not by the mouse but with code, there is already an answer in this link How to trigger event in JavaScript?

<div id="abc">Click me</div>

<script> document.getElementById.addEventListener('mouseup', function () { alert('mouseup'); }); </script>

getElementById missing the param here i.e getElementById('abc')

document.getElementById('abc').onmouseup();

onmouseup() is not a function its an Event attributes and should be called on some element.

$('#x').trigger('mouseup');

Should be done something like this :

$( "#abc" ).click(function() { $( "#x" ).mouseup(); });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744227156a4564064.html

相关推荐

  • javascript - Trigger mouse-up programmatically - Stack Overflow

    I would like to trigger a mouseup event programmatically given:<div id="abc">Click me&l

    9天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信