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');
Related
This question already has answers here:
What is caret symbol ^ used for in css when selecting elements?
(4 answers)
Closed 1 year ago.
I have come across some JS/jQuery code and am trying to find out what the ^= operator means. I can't find anything like this when Googling it.
The HTML page contains a form with these input fields:
<input type="text" id="name" name="name[1]" value="" size="22"/>
<input type="text" id="name" name="name[2]" value="" size="22"/>
<input type="text" id="name" name="name[3]" value="" size="22"/>
The JS validates the form before sending it to the server. Can somebody explain to me how the jQuery selector that is used in the lines with val and each works (or point me to the documentation)?
jQuery("button#finish").click(function(e) {
var error = false;
e.preventDefault();
if (jQuery("[name^=name]").val() != "" ) {
jQuery("[name^=name]").each(function(){
if (jQuery(this).val() == "") { jQuery(this).closest("tr").remove(); }
});
if ( ! error) { jQuery("#form_names").submit(); }
}
else { alert("Bitte mindestens eine Person zuoberst in das Formular eintragen."); }
});
The jQuery() function (and it's alias the $() function) take a CSS selector as an argument to determine which element(s) to operate on. The list of supported selectors is given in the jQuery API documentation.
Here, the selector [foo^="bar"] means:
[an] element whose "foo" attribute value begins exactly with the string "bar"
This question already has answers here:
toggle checkbox with javascript
(4 answers)
Closed 2 years ago.
Very basic question here, I am using a JS library to add keyboard shortcuts, I want to check and uncheck a checkbox using the same line of commands, but I am not sure about how to do this, here is the code:
shortcuts.add('alt+s', function() {
document.getElementById("menu-toggle").checked = true
})
How can I use the same shortcut to uncheck the same element?
document.getElementById("menu-toggle").checked = false;
Thanks in advance!
I recommend Element.toggleAttribute:
const toggleChecked = () => document.getElementById('menu-toggle').toggleAttribute('checked');
document.getElementById('myBtn').addEventListener('click', toggleChecked);
<input id="menu-toggle" type="checkbox"> Check me
<br><br>
<button id="myBtn">Click me</button>
This question already has answers here:
onKeyPress Vs. onKeyUp and onKeyDown
(14 answers)
Closed 2 years ago.
So, I have this sample code:
<textarea id="input" onkeydown="pressed()"></textarea>
<div id="output"></div>
<script>
function pressed()
{
var input = document.getElementById('input').value;
document.getElementById('output').innerHTML = input;
}
</script>
My problem is, if type something on the input, the script wont write it on the output immediately, it only does after I type another key.
So, is there anything that im not doing right?? Help pls
Use the input event - then paste will also work
document.getElementById("input").addEventListener("input", function() {
document.getElementById('output').innerHTML = this.value;
})
<textarea id="input" ></textarea>
<div id="output"></div>
Because you're using onkeydown which triggers function pressed immediately, and input will equal to the value of input before you press.
Use onkeyup and it will work fine.
This question already has answers here:
Assign click handlers in for loop
(6 answers)
Closed 3 years ago.
So my goal is to replace text in input fields using my javascript/jquery code. I want to target each input field by incremented ID using a For Loop, but my code is not working?
for(i = 1; i < 3; i++) {
$("#inp"+i).on('change keyup paste input',function(){
var a=$(this).val();
var b=a
.replace(/hey/g,'hello');
$(this).val(b);
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="inp1">
<input type="text" id="inp2">
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";