0

I'm trying to add this class with jQuery on a click of a button. I have to add/remove it, can't quite figure out the exact jQuery code for that. Using jQuery 1.12.

Important note - I have to toggle between the current CSS for the focus status, and the changed CSS (border added). This has to happen to all of the elements on the page!

The jQuery code:

$("#button").on('click', function() {
//jQuery Code needed to add the class/style.
}

CSS code:

*:focus {
    border:3px solid black !important;
}
Yossi
  • 1,046
  • 2
  • 13
  • 20
  • 1
    `*` means every element gets a border when focussed. – Mouser Mar 03 '17 at 12:18
  • You would need to focus onto the element: `$(this).focus();`. If you want to be able to use these styles with a class name just change your declaration to `*:focus, .do-focus {` then you can use `$(this).addClass('do-focus')` – Kallum Tanton Mar 03 '17 at 12:18
  • So "#button" clicked you want to add border to all the elements? – Omprakash Arumugam Mar 03 '17 at 12:41
  • Yes, only when the elements are focused! That means I can toggle the focus state of all of the elements on the web page with a button. One time with a border, and one time without. – Yossi Mar 03 '17 at 13:41

5 Answers5

1

I would do:

$("#button").on('click', function() {
   $('div').addClass('focus'); // or toggleClass
}

.focus { 
  border:3px solid black !important;
}

Read more: addClass(), removeClass(), toggleClass()

Fred Randall
  • 7,544
  • 21
  • 83
  • 184
0

.css( propertyName, value ) --> http://api.jquery.com/css/

$("#button").on('click', function() {
   $("holdTheItem").css("border", "12px");
}
NewPHPer
  • 233
  • 6
  • 19
0

I would only change the class of the body.

document.querySelector('#button').addEventListener('click', function(e) {
   document.body.classList.toggle('-changedFocus');
});

and for the css

body.-changedFocus *:focus {
  // your changes
}
cyr_x
  • 13,387
  • 2
  • 31
  • 45
0

Eventually I used java script to bind the focus event, and used add class/remove class to handle this.

document.addEventListener('focus',function(e){
 var focused = $(':focus');
 focused.addClass('className');
 focused.focusout( function() {
  $(this).removeClass('className');
     });
}, true);
Yossi
  • 1,046
  • 2
  • 13
  • 20
-2

You can use * selector.

FIDDLE

Here is the code

$("button").on('click', function() {
var elementCount = $( "#test" ).find( "*" ).css( "border", "3px solid red" ).length;
});
h2,
  span,
  p {
    width: 80px;
    height: 40px;
    float: left;
    padding: 10px;
    margin: 10px;
    background-color: #EEEEEE;
  }
   
<div id="test">
  <h2>H2</h2>
  <span>SPAN</span>
  <p>P</p>
</div>
<button>Button</button>
shubham agrawal
  • 3,035
  • 4
  • 19
  • 31