I want to disable my all input element of a div by applying my custom css class.
but i could not find any css attribute which can disable an input element.
currently what am i doing
$('#div_sercvice_detail :input').attr('disabled', true);
$('#retention_interval_div :input').addClass("disabled");
which can disable all input element of div with css attr but i want to apply my custom class for disable all input with some extra css attributes
$('#retention_interval_div :input').addClass("disabled");
class
.disabled{
color : darkGray;
font-style: italic;
/*property for disable input element like*/
/*disabled:true; */
}
any suggestion for doing this with jquery without using .attr('disabled', true);?
There is no way to disable an element with just CSS, but you can create a style that will be applied to disabled elements:
<style>
#retention_interval_div​​ input[type="text"]:disabled {
color : darkGray;
font-style: italic;
}​
</style>
Then in your code you just have to say:
$('#retention_interval_div :input').prop("disabled", true);
Demo: http://jsfiddle.net/nnnnnn/DhgMq/
(Of course, the :disabled CSS selector isn't supported in old browsers.)
Note that if you're using jQuery version >= 1.6 you should use .prop() instead of .attr() to change the disabled state.
The code you showed is not disabling the same elements that it applies the class to - the selectors are different. If that's just a typo then you can simplify it to one line:
$('#retention_interval_div :input').addClass("disabled").attr('disabled', true);
You can use following css to practically disable the input:
pointer-events: none;
Using normal CSS
.disabled{
opacity: 0.6;
cursor: not-allowed;
pointer-events: none;
}
No. CSS can't disable an input element. CSS is for styling only, it can't do anything else. Also, input will only work with input, don't forget select, textarea, password
You need to do 2 things, so why not wrap them in a single function? You could even create a little plugin to do this:
(function ($) {
$.fn.disableInput = function () {
return this.each(function(){
$(this).prop('disabled');
$(this).addClass('disabled', true);
});
}
})(jQuery);
Then you can call it like this:
$('#myInput').disableInput();
...and even chain it with other stuff, like this:
$('#myInput').disableInput().addClass('otherClass');​
You can't disable input elements using css properties. But you can improve your current coding like following
$('#div_sercvice_detail :input').prop('disabled',true).addClass("disabled");
Related
This question already has answers here:
Can I have an onclick effect in CSS?
(14 answers)
Closed 6 years ago.
In javascript, you can easily execute onclick events directly from the html element:
<div onclick="..javascript here.."></div>
I know that you can change the css styles with the <style> tag, but I was wondering if you were able to execute it similarly to the example below:
<div onclick="..css here.."></div>
if you want to do it purely through css you have to use :active or maybe :focus:
div:hover { color: red; } /* mouse-over */
div:active { color: red; } /* mouse-down (this cause also focus) */
input:focus{ color: red; } /* got focus (by tab key or mouse-down) */
/* for <a> element: */
a:link { color: red; } /* unvisited links */
a:visited { color: red; } /* visited links */
Note: the :active does not stay permanent after the user release the mouse button for elements that does not take focus (like as a div) but it works for elements like as text inputs or buttons. there is a workaround for it called "Checkbox Hack" where you use a connected label and checkbox input and some other element you are trying to control..
Also, if you want to change css class or inline styles, you could do as following:
<div onclick="this.style['border'] = '2px solid red';">Click me</div>
There is, but the element needs to have a tabindex attribute.
With a tabindex on the element you can use:
element:focus {
/* some_CSS; */
}
'some_CSS' will kick in when the element is clicked.
You can use javascript to change the style of a div or any other element. But I donot know whether there is a way to change css by onclick event without using javascript.
I can explain my method.
<script>
function change_css(){
document.getElementById('change_css').className='newClassName';
}
</script>
<div onclick="change_css()" class="initial_class">content</div>
The above code will help you change the style by changing the class, provided you have already created a class with css. It replaces all the previously provided classes for that div and add the new one.
To add an additional class to the div without replacing the existing classes, use the following statement in javascript:
document.getElementById('change_css').className+=' newClassName';
Does anyone know, is it possible to show outline around input element when user is using sequential navigation (TAB button) and hide outline when user clicks this input element with the mouse? Has anyone implemented this kind of behaviour?
I`m using this property on my :focus selector in CSS file:
:focus {
outline: #00bfff solid 1px !important
}
Currently, outline appears when input element is focused.
BR,
Raimonds
Just blur it on click.
jQuery(document).ready(function() {
jQuery('input').on('click', function() {
jQuery(this).blur();
});
});
This should remove the focus from the input when clicked, thus un-triggering your css rule for :focus, while it will still be applied if your input gets the focus by keyboard navigation.
Edit: Just tried it in Chrome/Windows 7, it doesn't seem to achieve what it is supposed to.
If someone wants to give it a ride to find a working solution, here's a pen:
http://codepen.io/anon/pen/yNMoJv
Here is a fiddle https://jsfiddle.net/hyauyovm/2/
Hope it solves your problem
//HTML
<input>
<input>
<input>
<input>
//JS
<script>
$("input").click(function(){
$(this).addClass('focus');
});
$("input").blur(function(){
$(this).removeClass('focus');
});
</script>
//CSS
input:focus{
outline:2px solid blue;
}
input.focus{
outline:none;
}
you can do this either in js or css for js you have to use blur or in css you can use :focus. Here is an example of both.
using css
using css in fiddle
using js
using js in fiddle
for your case use this
Fiddle
Try this,
input:focus {
outline: #00bfff solid 1px !important
}
I'm trying to remove the value of an inherited CSS property using Jquery.
.class1 #id1 div.class2 input.class-input{
border-color: #bbb3b9 #c7c1c6 #c7c1c6;
}
Anyone tell me how to remove this "border-color".
Thank.
Create a new class for example
.new_class{
border-color: #00ffdd !important;
}
!important does the trick!
Check this
You can use jQuery, but you'll have to assign a value to the border-color property. You can use transparent though:
$('.class-input').css('border-color', 'transparent');
Edit: Or you can disable the whole border:
$('.class-input').css('border', 'none');
You can either swap the on your div to change the color, or set the border color to empty using
$(".class1").css("border-color", "");
But I would recommend swapping out the class using the removeClass and addClass JQuery functions.
If you still want to keep the width of the border:
border-color: transparent;
If you want to remove the border all together
border: 0;
Edit: border: none; will give your the same result
So your jquery could look something like this:
$(".class-input").css("border","0");
However I would suggest using CSS if you don't need to make it animated. Since you raised the concern about .class1 #id1 div.class2 input.class-input.myclass (I'm assuming that's what you mean since you wouldn't be throwing a div into an input box.
You can use the CSS pseudo-selector :not
.class1 #id1 div.class2 input.class-input:not(.my-class){
border: 0;
}
The simplest way to handle this is to add another reference to give your override code a higher specificity.
.class1 #id1 div.class2 input.class-input [#MyNewID]{
border: none;
}
This removes the border for the area where you have added the ID so that if you are using this same format in other pages you can add an additional ID on the element on the page where you want the border to "disappear"
Please don't use !important this is a lazy way to override code and is not necessary 95% of the time. It will also cause you problem later when you are trying to change this if you are pushing down site wide skins.
When I use .prop('disabled',true) to disable a button, it works, but the button does not look disabled. I remember in the old days when I used .attr('disabled','disabled') to disable buttons, they would become more visibly disabled, i.e. the text would be greyed out or something so the user wouldn't try to click. Now I think the button border fades a bit but the text is not.
What's the easiest way to get the old behavior back? I am lazy and don't want to write one line of code to disable the button and another to make it look disabled - I want to get both effects in a single command if possible. Should I use a different element other than a button? A different method of disabling?
I made a fiddle here: http://jsfiddle.net/ak2MG/. Here's the code.
HTML:
<button type='button' id='mybutton'>Click Me</button>
<div id="mydiv"></div>
Javascript:
$('#mybutton').click( function() {
$('#mydiv').append("<p>Button was clicked.</p>");
$('#mybutton').prop('disabled',true); } );
Or change the opacity of the button
$('#mybutton').click( function() {
$('#mydiv').append("<p>Button was clicked.</p>");
$('#mybutton').prop('disabled',true).css('opacity',0.5);
});
Fiddle
I would add a disabled class to the button.
This lets you control the styling from CSS instead of javascript so all of your styling is in one place (where it should be).
Demo: JSFiddle
HTML
<button type='button' id='mybutton'>Click Me</button>
<div id="mydiv"></div>
JS
$('#mybutton').click( function() {
$('#mydiv').append("<p>Button was clicked.</p>");
$('#mybutton').prop('disabled',true).addClass('disabled');
});
CSS
.disabled {
color: #999;
}
it is pretty simple, just change the text style
$('#mybutton').click( function() {
$('#mydiv').append("<p>Button was clicked.</p>");
my_button_disable(this);
});
function my_button_disable(btn) {
$(btn).prop('disabled',true);
$(btn).css('color', 'lightgray');
// put whatever else you want here
}
http://jsfiddle.net/ak2MG/6/
Simplest - Add a state in CSS
Target it with CSS to change the style,
importantly the pointer-events: none will make it unresponsive. :
button:disabled {
background: #F5F5F5;
color : #C3C3C3;
cursor:none;
pointer-events: none;
}
The change from attr() to prop() was only to the jQuery API and has nothing to do with any difference you are observing in the style of a disabled button.
A disabled button's style is decided by the browser. The fiddle you provided looks very "disabled" in Google Chrome (Version 33.0.1750.154 m). If you'd like to alter the style of a disabled button to your liking, I recommend adding a class OR styling based on attribute
button[disabled],
button.disabled {
color: #999;
background: #DDD;
border: 1px solid #CCC;
}
In JQuery UI, there are a lot of CSS double classes, for example for JQuery UI's tabs
.ui-widget-content .ui-state-default
{
border: 3px solid #EEEEEE;
background: #ffffff url(BGDel.png) repeat-x;
font-weight: bold;
color: #ffffff; outline: none;
}
The above works fine if it is in a CSS file.
I want to use javascript / Jquery to change one of property, like
$(".ui-widget-content .ui-state-active").css({"font-weight":"normal"} );
It doesn't work. Could anyone help how to set or change the CSS double class properties through script? Thanks.
$(".ui-widget-content .ui-state-active").css({"font-weight":"normal"} );
This line does not change the CSS class itself, it only selects an element matching this selector .ui-widget-content .ui-state-active.
So please do an "Inspect element" for example in firebug or Google Developer Tools, find needed element, and see if you are using the right selector.
Since .css() function puts all given code into style attribute, it should override any class that is in CSS file, so i think, that you should try a different selector to match your element.
There are two class apply on your element first is .ui-widget-content and second is .ui-state-default.
in your example you set css to .ui-widget-content or .ui-state-active. but it is not correct because the a another css .ui-state-default apply on that element.
so you need to use !important or also apply jquery code for.ui-state-default class
try -
$(".ui-widget-content .ui-state-active").css('font-weight', 'normal!important' );
or
$(".ui-state-default").css('font-weight','normal'); // for fire fox use css('font-weight', '400');
or
$(".ui-widget-content .ui-state-active .ui-state-default").css('font-weight', 'normal');