Select inputs that doesn't have value attribute with querySelector - javascript

I'm using:
document.querySelectorAll('input[value=""]')
But it's not selecting inputs that doesn't have the attribute value.
How can I select all inputs that doesn't have a value '', and that doesn't have the attribute value?
I would like to do that without jQuery and without any additional function, would that be possible only using querySelectorAll?

Try
document.querySelectorAll('input:not([value])')
This should return all the input that don't have the value attribute.

You can use this:
document.querySelectorAll('input:not([value]):not([value=""])');
get all inputs that doesn't have attribute "value" and the attribute
"value" is not blank
var test = document.querySelectorAll('input:not([value]):not([value=""])');
for (var i = 0; i < test.length; ++i) {
test[i].style.borderColor = "red";
}
<input type="text" />
<input type="text" value="2" />
<input type="text" />
<input type="text" value="" />
<input type="text" />

Related

How do I set the background color of a textbox to it's own value when the page loads

I thought this would work:
<input type="text" id="ThisIsDynamicAsItsInARepeater" OnLoad="this.style.backgroundColor=this.innerHTML"
But that gets me:
Identifier expected
I know that the value of the textbox is a valid HTML color value.
onload attribute won't work on inputs. you need to done it inside the javascript tag.
this should do the job :)
some inputs
<input type="text" id="ThisIsDynamicAsItsInARepeater" value="green" />
<input type="text" id="ThisIsDynamicAsItsInARepeater" value="black" />
<input type="text" id="ThisIsDynamicAsItsInARepeater" value="blue" />
JS
//wait for the page to load
window.onload = function(){
//target all inputs first
var input = document.getElementsByTagName('input');
for(var i = 0; i< input.length; i++){
//filter out inputs which have id attribute
if(input[i].hasAttribute("id")){
//now check if they have ThisIsDynamicAsItsInARepeater
if( input[i].getAttribute("id").indexOf('ThisIsDynamicAsItsInARepeater') >= 0){
//finally change color
input[i].style.backgroundColor = input[i].value;
}
}
}
}
</script>

Disabling textarea and input at the same time

I disable a textarea like in this code snippet:
function toggleDisabled(_checked,id) {
document.getElementById(id).readOnly = !_checked;
}
<input type="checkbox" name="no" value="1" onchange="toggleDisabled(this.checked,'new_order')">
<textarea name="noa" id="new_order">FOOO</textarea>
<input type="text" name="noo" id="new_order">
Now I want input text to get disabled when I check the checkbox. So that both textarea and input:text will be disabled.
I tried to add the id that I used as ID for textarea but input:text and textarea are conflicted since readOnly is only for textarea.
So that I need a way tp say if textarea disable like this... , if input disable like this ...
id must be unique! Never use same id to more than one element.
the code below selects all elements that have the class new_order and then iterate through then disabling or enabling then. Take a look
function toggleDisabled(self) {
var inputs = document.getElementsByClassName('new_order');
for (var i = 0; i < inputs.length; i++){
inputs[i].disabled = self.checked;
}
}
<input type="checkbox" name="no" value="1" onchange="toggleDisabled(this)">
<textarea name="noa" class="new_order">FOOO</textarea>
<input type="text" name="noo" class="new_order">
The problem arises when you use an id multiple times... ID's are supposed to be unique. If you use classes it should work:
function toggleDisabled(_checked, selector) {
document.querySelectorAll(selector).forEach((el) => {
el.readOnly = _checked;
});
}
<input type="checkbox" name="no" value="1" onchange="toggleDisabled(this.checked,'.new_order')">
<textarea name="noa" class="new_order">FOOO</textarea>
<input type="text" name="noo" class="new_order">

How to get html input in JavaScript?

I am using the code below in a html form:
<input type="text" name="cars[]" required>'
Note the use of "cars[]" for the name.
This allows me to have multiple inputs with the same name.
I would like to get the answers from all the inputs in JavaScript.
How can this be done?
I have the following WRONG code for this:
var element = document.getInput("cars[]");
for (i = 0; i < element.length; i++) {
alert(element[i].value);
}
You have to use document.getElementsByName() like this:
var element = document.getElementsByName("cars[]");
for(i=0; i<element.length;i++){
alert(element[i].value);
}
<input type="text" name="cars[]" value="a" required>
<input type="text" name="cars[]" value="b" required>
<input type="text" name="cars[]" value="c" required>
These two things in pure JavaScript net approximately the same result. The first is using the HTML form element to find all of the input elements attached to it. However, the syntax for finding the array called "cars[]" is troublesome and in my opinion a tad annoying. If I was going to do something in pure JavaScript I'd probably prefer the second way, using document.querySelectorAll.
window.addEventListener('load', function() {
var form = document.getElementById('thing');
form.elements['cars[]'].forEach(function(el, i) {
console.log("value is ", el.value)
}); //Form.elements[] array has been available since Chrome 7 or so. It should be available for use in just about any browser available.
var items = document.querySelectorAll('[name="cars[]"]');
items.forEach(function(el, i) {
console.log("Item Value is ", el.value)
});
});
<form id="thing">
<input type="text" name="cars[]" value="1" />
<br />
<input type="text" name="cars[]" value="2" />
<br />
<input type="text" name="cars[]" value="3" />
<br />
<input type="text" name="cars[]" value="4" />
<br />
<input type="submit" value="submit" />
</form>
You write
Note the use of "cars[]" for the name.
This allows me to have multiple inputs with the same name.
In HTML, you can have many inputs in the same form with the same name, regardless of that name having a [] suffix or not. This has always been used for, say, checkboxes. Most server-side libraries will then return the values for those inputs as an array.
An example of gathering all values for inputs with a given name could be the following:
document.querySelector("#b").addEventListener("click", () => {
const values = [];
document.querySelectorAll("input[name='color']").forEach(e => values.push(e.value));
console.log(values); // outputs ["foo", "bar", "baz"] if values unchanged
});
input { display: block; margin: 5px; }
<label>Enter your favorite colors
<input type="text" name="color" value="foo"/>
<input type="text" name="color" value="bar"/>
<input type="text" name="color" value="baz"/>
</label>
<label>
Enter your favorite food
<input type="text" name="food" value="flub"/>
</label>
<button id="b">Click me to output favorite colors</button>
You can give same id to all inputs like
<input type="text" id="inputId" name="cars[]" required>'
In Javascript iterate the element to get the value
var element = document.getElementsByName("cars[]");
for(i=0; i<element.length;i++){
console.log(element[i].value);
}

Dynamically Create an ID based on an Input Name

I'd like to know how to dynamically generate an ID on every input tag based off of it's name. I have to do this using javascript only - Not jQuery.
So, for example, if I have the following text inputs:
<input type="text" name="input1" value="">
<input type="text" name="input2" value="">
<input type="text" name="input3" value="">
I'd like to end up with this:
<input id="input1" type="text" name="input1" value="">
<input id="input2" type="text" name="input2" value="">
<input id="input3" type="text" name="input3" value="">
Any help would be appreciated,
Thanks!
In plain JS, you can get the elements basis of tag name.
And then set the id attribute for that element
function setinputIds() {
var inputs = document.getElementsByTagName('input');
for(var i=0;i < inputs.length; i++) {
var id = inputs[i].getAttribute('name');
inputs[i].setAttribute("id", id);
}
}
var inputs = document.getElementsByTagName('input');
for (var ii=0, item; item = inputs[ii]; ii++) {
item.id = item.name;
}
Probably you can use a for loop to iterate name and insert id like this:
for(i=1;i<4;i++){
document.getElementsByName('input'+i).id='input'+i;
}

Can not pass html array to JS

Hi I'm having trouble sending html form array to JS. I have dynamic html form and some fields:
<div id="fields">
<input type="text" name="ppav[]" id="p" />
<input type="text" name="quantity[]" id="q" size="3" />
<input type="text" name="price[]" id="pr" size="10" />
<input type="text" name="sum" id="su" size="10" disabled="disabled"/>
<br />
</div>
in JS i tried using this function but getting undefined alert instead of result:
var welements = document.getElementsByName('ppav[]');
for (var i = 0, j = welements.length; i < j; i++) {
var an_element = welements[i];
alert(an_element.selectedIndex);
}
What did you expect? the selectedIndex property returns the index of the selected option in a select element. If you want the value of your input fields, try alert(an_element.value);

Categories

Resources