JavaScript Prevent focus to URL bar with CTRL + L prevent focus with css,prevent focus on click,prevent focus

I'm working on a terminal-like modal, using bootstrap's and I want to clear it when the user presses Ctrl+L like it does when he writes clear in it.
But the problem is, when I press Ctrl+L, it focuses the URL bar, and I don't have anymore focus on my page.
In this part, alert is showing, but it still focuses URL bar after :

if(event.key == "l" && event.ctrlKey)
    alert("clear it");

I have tried several things like focusing my terminal after a sec, or things like that. But nothing worked.
Have you got any ideas ? Thanks you !

Answer:1

Well, it wasn't hard as I thought it would be.
The only thing which worked was e.preventDefault(), I tried return false but it didn't.

if(event.key == "l" && event.ctrlKey)
{
    event.preventDefault();
    // ...
    // what I wanted to do
}

Thank you all for your quick answer and Pawan for yours in particular.

Answer:2

I agree with Barmar, but here you go:

document.addEventListener("keydown", function(e) {
if ((navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey) && e.keyCode === 76) {
    e.preventDefault();
    return false;
}

return true;
});

http://jsfiddle.net/L8L23u9v/

Answer:3

I am trying to place an element, "inner", flush against the upper right hand corner of another div, "outer", using absolute positioning. The "inner" element is not a child of "outer". I therefore need ...

I am trying to place an element, "inner", flush against the upper right hand corner of another div, "outer", using absolute positioning. The "inner" element is not a child of "outer". I therefore need ...

  1. place element inside div
  2. jquery place element inside div
  3. javascript place element inside another
  4. jquery place element inside another

How do I create a image map with separate <select>? I need something like this: <img src="image.png" alt="Website map" usemap="#mapname" /> <map name="judete" id="judete"> <...

How do I create a image map with separate <select>? I need something like this: <img src="image.png" alt="Website map" usemap="#mapname" /> <map name="judete" id="judete"> <...

  1. image with select option
  2. change image with select option
  3. image select
  4. image select sticker instagram
  5. image select sticker instagram missing
  6. image select html
  7. image select bootstrap
  8. image select jquery
  9. image select css
  10. image select sticker instagram android
  11. image select instagram story
  12. image select box
  13. image select checkbox
  14. image select sticker
  15. image select js
  16. image select from gallery android
  17. image select react
  18. image select javascript
  19. image select dropdown
  20. image select underwood

Very simple example: var a = { id: 5 }; var b = { id: 6 }; var c = { id: 7 }; var arr = [a, b, c]; Now i have a function: function remove(startIndex) { // set objects to null from startIndex in ...

Very simple example: var a = { id: 5 }; var b = { id: 6 }; var c = { id: 7 }; var arr = [a, b, c]; Now i have a function: function remove(startIndex) { // set objects to null from startIndex in ...

  1. access via null object reference
  2. access vba is null object required

<ul class="todo-list"> <li ng-repeat="todo in todos">{{todo.todoContent}}<span class="pull-right glyphicon glyphicon-check" ng-class="{checked:todo.done}" ng-click="todoCheck(todo)" ...

<ul class="todo-list"> <li ng-repeat="todo in todos">{{todo.todoContent}}<span class="pull-right glyphicon glyphicon-check" ng-class="{checked:todo.done}" ng-click="todoCheck(todo)" ...

  1. change target attribute jquery
  2. change target attribute javascript
  3. setattribute target _blank
  4. set attribute target
  5. set target attribute in javascript
  6. set target attribute in jquery
  7. set target attribute html