How to simulate a Ctrl + Click on a link?

1

I want to simulate a Ctrl + Click on a link to open another tab but in the browser does not follow that new tab, I mean? I want to stay on the same tab. The example and problem:

<a class="btn btn-small" href="/en/upload-file" data-original-title=""><i class="icon-white icon-plus"></i> Add new</a>

I have this Script that I can not adapt

var e = jQuery.Event("click"); e.ctrlKey = true; $('#id').trigger(e);

As you can see this script works with #id but this outerHTML of example does not have #id I wonder if it can be done taking as a reference the class= or directly the href= as you would solve it?

    
asked by Stack_qwerty 18.10.2017 в 07:03
source

2 answers

2

Good use target="_ blank" as an attribute in link a.

<a class="btn btn-small" href="/en/upload-file" target="_blank" data-original-title=""><i class="icon-white icon-plus"></i> Add new</a>

If that still is not what you are looking for, you can search for it using the value of href in case you can not add a new Id or Class:

var e = jQuery.Event("click");
e.ctrlKey = true;
$('a[href="/en/upload-file"]').trigger(e);

or if you want to search for it through the class:

var e = jQuery.Event("click");
e.ctrlKey = true;
$('a.btn-small').trigger(e);
    
answered by 18.10.2017 в 09:16
0

to see if this fits you, with whatever you tell me:

            var j = jQuery.Event('keydown');
            j.ctrlKey = true;  
                if(j){
                    $('a.btn.btn-small').attr("target","_blanck");
                    $('a.btn.btn-small')[0].click();

                    }
    
answered by 11.01.2019 в 22:15