This question already has answers here:
Changing CSS pseudo-element styles via JavaScript [duplicate]
(8 answers)
Closed 5 years ago.
I have this CSS: input::placeholder {font-size: 30px}
and I trying to change input placeholder CSS in JS
var inputPlaceholder = document.querySelector("input::placeholder");
inputPlaceholder.style.fontSize = "5px";
but console returns Uncaught TypeError: Cannot read property 'style' of null.
How to to change input placeholder CSS in the right way?
Try For This:
<input type="text" style="font-size: 30px" placeholder="Here." id="new"/>
var inputPlaceholder = document.getElementById('new');
inputPlaceholder.style.fontSize = "5px";
Related
This question already has answers here:
Get "original" (non-hover) background color of object when hovering over it
(6 answers)
Closed 4 years ago.
I'm trying to get the background color of the clicked element, but I get the value for: hover. This is my code:
$(button).click(function() {
var bgColor = $(this).css('background-color');
console.log(bgColor);
});
What am I doing wrong?
You can use event.target
$(button).click(function(e) {
var bgColor = $(e.target).css('background-color');
console.log(bgColor);
});
This question already has answers here:
What are alternatives to document.write?
(11 answers)
Document.write function replaces the whole page with the text
(2 answers)
Closed 5 years ago.
Whenever i tried using document.write it replaces the current html page content in example i have this HTML
<body>
<div>
<h1>Hello</h1>
</div>
and this jQuery
var notif = document.write("<div class = 'bg' style = 'height:250px; width:400px; z-index:10; background:red;'>Good Morning!</div>");
$('body').append(notif).delay(5000).fadeOut()
this replace the whole page big Hello will be gone
the jQuery will work after a 5 seconds it disappears then displays nothing?
document.write is not needed for 'append' (document.write writes the content to the page replacing the existing content fully). Pls see the documentation for jquery append: http://api.jquery.com/append/
You'll have to just write
var notif = "<div class = 'bg' style = 'height:250px; width:400px; z-
index:10; background:red;'>Good Morning!</div>";
$('body').append(notif).delay(5000).fadeOut()
This question already has answers here:
How to update placeholder color using Javascript?
(5 answers)
Closed 4 years ago.
I want to change the colour of this placeholder after calling mobileValidate().
<input type="text" class="form-control" name="InputMobile" id="Mobile" placeholder="Enter Mobile Number" onblur="mobileValidate()"required>
JavaScript function is
function mobileValidate(){
var x = document.getElementById("Mobile");
if ((x.value).match(re)){
alert("mobile Number is valid");
}
else{
alert("mobile no is not valid");
x.value="";
x.placeholder.style.color="red";
}
}
You can't really modify pseudo-selectors with JavaScript. You'll have to modify an existing a element.
If possible, make a class:
.your-class::-webkit-input-placeholder {
color: #b2cde0
}
And add it to the element:
$('input').addClass('your-class');
Or if you want to use pure JS, do this:
x.classList.add('your-class');
This question already has answers here:
How to add/remove a class in JavaScript?
(13 answers)
Closed 7 years ago.
I have the following classes (below), they are added dynamically on the HTML (below).
I have classes already present, what Javascript recommended code should be used to remove one or all of these classes if they are already present on the html below. The function I am building, will reset the element, I NEED the current classes to be present, but the colour classes need to be removed if one / multiple values are present.
HTML
<div id="overlay__inner" class="overlay__inner overlayActive clearfix"></div>
Classes
<ul>
<li>is--blue</li>
<li>is--red</li>
<li>is--yellow</li>
<li>is--purple</li>
<li>is--green</li>
<li>is--pink</li>
<li>is--orange</li>
</ul>
UPDATE:
So in a nutshell, I want to do this jQuery example in javascript:
$('#overlay__inner').removeClass('is--blue is--red is--yellow is--purple is--green is--pink is--orange');
This is what I have so far:
document.getElementById('overlay__inner').classList.remove('is--blue').classList.remove('is--red').classList.remove('is--yellow').classList.remove('is--purple').classList.remove('is--green').classList.remove('is--pink').classList.remove('is--orange');
But I get this error:
Uncaught TypeError: Cannot read property 'classList' of undefined
Removing a single class is rather straightforward, what are you having trouble with specifically?
var els = document.querySelectorAll('.is--blue');
Array.prototype.forEach.call(els, function(el) {
el.classList.remove('is--blue');
});
.is--blue {
color: blue;
}
<p class="is--blue something-else">Sample</p>
<p class="is--blue">Sample</p>
This question already has answers here:
Remove CSS class from element with JavaScript (no jQuery) [duplicate]
(11 answers)
Closed 8 years ago.
I'm using DOM and creating elements and CSS classes dynamically. How can I remove a CSS class that I have created previously using JavaScript only?
I use this code to create CSS class node:
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '.s1_o1{position:absolute;left:487px;top:243px;width:226px;height:61px;background-color:#ffffff;color:#ff0000;font: 18px Arial;}';
document.getElementsByTagName('head')[0].appendChild(style);
I want to remove the s1_o1 class.
For cross browser compatibility you will need to do something like this.
var head = document.getElementsByTagName('head')[0];
head.className = head.className.replace(/\bClassName\b/,'');