CSS class is missed after jQuery replaceWith. How to fix?
Tags: javascript,jquery,css,.net
Problem :
I added this line to a master.cs page.
<h4 class="hideAndShow">[ - ] Hide</h4>
In CSS file:
.hideAndShow
{
position: fixed;
top:120px;
right:420px;
color: white;
background-color:green;
}
Then this in the same master.cs page.
<script>
var hideShowBool = true;
$('.hideAndShow').bind('click', function ()
{
if (hideShowBool == true)
{
$(this).replaceWith('<h4 class="hideAndShow">[ + ] Show</h4>');
hideShowBool = false;
} else
{
$(this).replaceWith('<h4 class="hideAndShow">[ - ] Hide</h4>');
hideShowBool = true;
}
});
</script>
The expected behavior: The text [ - ] Hide should toggle between: [ + ] Show and [ - ] Hide.
But what happens is that it only responds to the very first click. In that [ - ] Hide is replaced with [ + ] Show. But when clicking it again; there is no effect as expected.
Where is the problem? and How to fix it?
Solution :
That's because you are replacing the element, so the new element doesn't have any click handler bound to it. You can either use the event delegation technique:
$('#aStaticParentElement').on('click', '.hideAndShow', function () {
Or change the text content of the element instead of replacing it:
$('.hideAndShow').on('click', function() {
$(this).text(function(_, currentText) {
return currentText === '[ + ] Show'
? '[ - ] Hide'
: '[ + ] Show';
});
});
CSS Howto..
The img tag is okay. The center tag is obsolete since about 1867. But you can easily use text-align: center as a style on the parent element of the logo....
How about this: Notice that I have't set a width on the box. It will work no matter what the width of the box is. FIDDLE Markup: <div class="box"> <div...
You can use css shapes. Read this http://webplatform.adobe.com/shapes/. You have to know that it's not cross browsers. http://caniuse.com/#feat=css-shapes EDIT - there is polyfill by Adobe, check this out. https://github.com/adobe-webplatform/css-shapes-polyfill So...
Don't use a wrapper around the entire site in your HTML/CSS template setup so the wrapper around your flexslider can actually reach to the edges of the browser window...
Adding a padding-bottom or margin-bottom seems to be working. li:hover{ padding-bottom: 10px; } Here's a FIDDLE or li:hover{ margin-bottom: 10px; } Here's a FIDDLE Update: After reading your comment and...
I am not sure if I understand entirely but to get the blotter to overlap the footer you can add this css: #section-foot { position: relative; top: -80px; } From...
The problem that I'm seeing, is that the onload event for the section tag isn't firing. You should add your javascript as a self-executing anonymous function to the end of...
To scroll horizontally you simply change the top values in the animations and #slider li to lefts (I copy it over to Word and use the replace function there), Demo...
Look at this: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta http-equiv="imagetoolbar" content="false"> <meta...
Use JavaScript: document.getElementById("target").onclick=function () {//Detects a click on #target if (this.classList) {//check if classlist is supported this.classList.add("newcolor");//add the class } else { this.class += " newcolor";//if classlist isn't supported, add...
you can not manipulate the DOM with CSS, you need JS or something similar. You will need to try something like this: $("#id-of-first-element").append( $("#id-of-last-element") ); $("#id-of-last-element").html(''); ...
Firefox / Webkit: -webkit-transform: rotate(-90deg); -moz-transform: rotate(-90deg); IE: filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3); ...
Answer updated: check this fiddle, hope this is exactly what you are looking for, //html <div class="nesting"> <a href="#" class="foo-class nesting-item"><span class="glyphicon glyphicon-folder-open" area-hidden="true"></span> Foo <span class="pencil glyphicon...
How to override a 's CSS when loaded by ajax?
Try this. CSS table.cb_table-hover > tbody > tr:hover > td:not(.override), table.cb_table-hover > tbody > tr:hover > th:not(.override){ background-color: #cfe2e8; } let me know if this helps you. I have just...
.sticky { position: fixed; width: 840px; margin-left: -420px left: 50%; top: 0; z-index: 100; border-top: 0; } This is the solution....
One solution would be to use two angled linear-gradient strips - one on the left and other at bottom of the button element. Imagine placing at 10px x 10px square...
Invalid CSS syntax. Do it this way: @page hide { visibility: hidden; margin: 0 15px; } .myreceipt { page: hide } By the way, what you should be using is...
Well, actually I was trying to do in the worst way. My layout is liquid so everything is defined with "%". When I was trying to reposition the elements, I...
The negation pseudo-class seems to be what you are looking for. table:not(.dojoxGrid) {color:red;} It's not supported by ≤ IE8 though....
Try this CSS: a:active, a:selected, a:visited { border: none; outline: none; } Note that this has to be after any a:hover rules. Thanks to PEra in the comments for suggesting...
Without using JavaScript and not having to support IE7 and below (IE8 fails on the second one) there are three options you can use: :first-child, :lastchild and the + selector:...
Use the following CSS2 selector: input[disabled=disabled] { /* your style */ color: #fff !important; } ...
This solved the Firefox cross domain font issue for me (which causes the font to not load in Firefox). Just add the following to .htaccess or directly to apache config:...
did you have a look at the css sac parser implementation? http://www.w3.org/Style/CSS/SAC/ --> http://www.w3.org/Style/CSS/SAC/doc/org/w3c/css/sac/Parser.html the document-handler has a comment method http://www.w3.org/Style/CSS/SAC/doc/org/w3c/css/sac/DocumentHandler.html#comment_java.lang.String_...
The most probable reason a plugin creator would want to add the js to the head from within the body is to ensure that anyone (even those who don't necessarily...
I solved it by using a negative margin and transitioning that with the same amount of time it takes for the transformation to transition (credit for solution goes to @GCyrillus's...
Div elements, by default, try to fit their container. so the middle one will try to fit its container which is the outer div.. it is not affected by content....
Try to apply css class to modal-body class inside $body.on('click', '[data-method]', function () {: var colorClass = $("input:radio[name=color]:checked").val(); $('#getCroppedCanvasModal').modal().find('.modal-body'). removeClass('original sepia gray').addClass(colorClass).html(result); ...
When you refer to an id or class in css, you must use the full name of the class or id you are selecting. For example, when you want to...
Just got it; .a{ background-color : red; height : 30px; width : 100px; overflow : hidden; position : absolute; } .b{ background-color : green; height : 30px; width : 100px;...
Try this. CSS table.cb_table-hover > tbody > tr:hover > td:not(.override), table.cb_table-hover > tbody > tr:hover > th:not(.override){ background-color: #cfe2e8; } let me know if this helps you. I have just...
.sticky { position: fixed; width: 840px; margin-left: -420px left: 50%; top: 0; z-index: 100; border-top: 0; } This is the solution....
One solution would be to use two angled linear-gradient strips - one on the left and other at bottom of the button element. Imagine placing at 10px x 10px square...
Invalid CSS syntax. Do it this way: @page hide { visibility: hidden; margin: 0 15px; } .myreceipt { page: hide } By the way, what you should be using is...
Well, actually I was trying to do in the worst way. My layout is liquid so everything is defined with "%". When I was trying to reposition the elements, I...
The negation pseudo-class seems to be what you are looking for. table:not(.dojoxGrid) {color:red;} It's not supported by ≤ IE8 though....
Try this CSS: a:active, a:selected, a:visited { border: none; outline: none; } Note that this has to be after any a:hover rules. Thanks to PEra in the comments for suggesting...
Without using JavaScript and not having to support IE7 and below (IE8 fails on the second one) there are three options you can use: :first-child, :lastchild and the + selector:...
Use the following CSS2 selector: input[disabled=disabled] { /* your style */ color: #fff !important; } ...
This solved the Firefox cross domain font issue for me (which causes the font to not load in Firefox). Just add the following to .htaccess or directly to apache config:...
did you have a look at the css sac parser implementation? http://www.w3.org/Style/CSS/SAC/ --> http://www.w3.org/Style/CSS/SAC/doc/org/w3c/css/sac/Parser.html the document-handler has a comment method http://www.w3.org/Style/CSS/SAC/doc/org/w3c/css/sac/DocumentHandler.html#comment_java.lang.String_...
The most probable reason a plugin creator would want to add the js to the head from within the body is to ensure that anyone (even those who don't necessarily...
I solved it by using a negative margin and transitioning that with the same amount of time it takes for the transformation to transition (credit for solution goes to @GCyrillus's...
Div elements, by default, try to fit their container. so the middle one will try to fit its container which is the outer div.. it is not affected by content....
Try to apply css class to modal-body class inside $body.on('click', '[data-method]', function () {: var colorClass = $("input:radio[name=color]:checked").val(); $('#getCroppedCanvasModal').modal().find('.modal-body'). removeClass('original sepia gray').addClass(colorClass).html(result); ...
When you refer to an id or class in css, you must use the full name of the class or id you are selecting. For example, when you want to...
Just got it; .a{ background-color : red; height : 30px; width : 100px; overflow : hidden; position : absolute; } .b{ background-color : green; height : 30px; width : 100px;...