jquery script adding id and using it doesn't work
Asked Answered
S

3

6

i am trying to use my keyboard to click on a button using the id.

For some buttons i had to set the ID, which actually works. But as soon as i try to use the keyboard for the buttons where i set the id, it won't work.

I am not getting any errors and since adding the id to the element works, i am kinda confused why i cannot use the new set id later in the code.

//setting id for first button (works)
$( "a:contains('Im Verband freigeben')" ).attr('id', 'freigabe-verband');
//setting id for second button (works aswell)
$( "a:contains('Vorheriger Einsatz')" ).attr('id', 'vorheriger-einsatz');


$( document ).keydown(function(e) {
 if (e.keyCode == 39) {
    //works, id is available, not set by me
     $("#alert_next").click();

} else if (e.keyCode == 38){
    // doesn't work, but id is set
    $("#freigabe-verband").click();

} else if (e.keyCode == 40){
    // doesn't work, but id is set
    $("#vorheriger-einsatz").click();

} 
return e.returnValue;

});

Anyone know why? https://i.sstatic.net/nQtRc.png

Synsepalous answered 16/12, 2015 at 15:24 Comment(5)
The DOM doesn't know that you have given that object an id, you need to delegate the event.Ripley
This works fine for me: jsfiddle.net/es8oxgjc . But I'm executing the jQuery onload in that fiddle. Can you wrap it in a document.ready and see if it works for you then?Ingenuous
If you are trying to simulate a anchor click see this link. #774139Consols
your code does work for any other handlers that are bound. jsfiddle.net/SeanWessell/s2p4wd48Consols
Well yeah, that makes sense. Not sure how to delegate it, gotta look it up.Synsepalous
O
3

To click a dom element, use:-

$('#freigabe-verband')[0].click();

or

$('#freigabe-verband').get(0).click();

Example

$("a").attr('id', 'test');

$(document).keydown(function(e) {
	$('#test')[0].click();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a href="http://stackoverflow.com">link</a>
Occlusive answered 16/12, 2015 at 15:45 Comment(1)
The [0] was missing and fixed my code - i haven't changed anything else.Synsepalous
B
1

It doesn't make sense. I copy your code and create a plunkr, it works well. http://plnkr.co/edit/Eb5QF6mB97Js41NFbr8J?p=preview

// Code goes here
$(function() {
  //setting id for first button (works)
  $( "a:contains('Im Verband freigeben')" ).attr('id', 'freigabe-verband');
  //setting id for second button (works aswell)
  $( "a:contains('Vorheriger Einsatz')" ).attr('id', 'vorheriger-einsatz');
  
  $( document ).keydown(function(e) {
   if (e.keyCode == 39) {
    //works, id is available, not set by me
     $("#alert_next").click();
  
  } else if (e.keyCode == 38){
    // arrow up
    $("#freigabe-verband").click();
  
  } else if (e.keyCode == 40){
    // arrow down
    $("#vorheriger-einsatz").click();
  
  } 
  return e.returnValue;
  
  });  
}
);
<!DOCTYPE html>
<html>

  <head>
    <script data-require="[email protected]" data-semver="2.1.4" src="https://code.jquery.com/jquery-2.1.4.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <h1>Hello Plunker!</h1>
    <a href="#" onclick="alert('a');">Im Verband freigeben</a><br>
    <a href="#" onclick="alert('b');">Vorheriger Einsatz</a>
  </body>

</html>
Boesch answered 16/12, 2015 at 15:51 Comment(0)
T
0

Try this example:

$(document).find("#vorheriger-einsatz").click();

You're adding to the DOM after the fact so unless you search the dom again it won't be found

Timeless answered 16/12, 2015 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.