Iterate through HTML inputs and set values - javascript

I am trying to iterate through some input elements and replace their values if empty.
$(document).ready(function() {
var input = $('.test').val();
var isValid;
$("input").each(function (){
var element = $(this);
if (element.val() == "") {
$('.test').val('empty');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<input class="test" type="text">
<input class="test" type="text">
<input class="test" type="text">
<input class="test" type="text">
<input class="test" type="text" value="test">
I want to change if a value is empty to "empty", but if it's not, to keep its own value. However it is setting every input to "empty" despite the last one having its own value. Can someone help me?

Use
element.val('empty');
Instead of
$('.test').val('empty');
Because $('.test').val('empty'); is setting value empty for all element that uses class test. As you want to change the value of a specific element, use that element reference instead of $('.test').
Here is your code with that little modification.
$(document).ready(function() {
var input = $('.test').val();
var isValid;
$("input").each(function (){
var element = $(this);
if (element.val() == "") {
element.val('empty');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<input class="test" type="text">
<input class="test" type="text">
<input class="test" type="text">
<input class="test" type="text">
<input class="test" type="text" value="test">

Hi you can check the attribute value and it should work.
$(document).ready(function() {
var input = $('.test').val();
var isValid;
$("input").each(function (){
var element = $(this);
if (element.attr('value') == "") {
$('.test').val('empty');
}
});

Related

How display data entered in one text box to another text box on checkbox tick?

I'm trying to reflect data entered in one text box to another text box on checkbox tick. The default state of checkbox is checked. The value should change after it is unchecked and gets checked back again. Even though the code seems to be working, the only output I'm getting is 'on'.
$(".check").click(function() {
if ($(this).is(":checked")) {
$('.add1').val(this.value) === $('.add2').val(this.value);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type=checkbox checked class="check">
<input type="text" id="first" class="add1" />
<input type="text" id="second" class="add2" />
Kindly note that I would like to do this by class.
Here's the fiddle: https://jsfiddle.net/starscream166/n87vLd51/1/
The issue is because this refers to the checkbox. Hence this.value, which you place in the value of the textboxes, it the string 'on'.
To fix this place the val() of .add1 in to .add2, as in the below example. Also note the use of change instead of click when dealing with checkboxes, as it improves accessibility.
$(".check").change(function() {
if (this.checked) {
$('.add2').val($('.add1').val());
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" checked class="check">
<input type="text" id="first" class="add1" />
<input type="text" id="second" class="add2" />
Please check here: https://jsfiddle.net/4kp3msax/1/
OR you can do the following code.
$(".check").click(function() {
if ($(this).is(":checked")) {
var add1 = $('.add1').val();
$('.add2').val(add1);
}
});
Pass $('.add2').val() the result of $('.add1').val()
.val() can be used to retrieve or assign an input's value. It can be called without a parameter, which will return the string value of the input. Or, it can be called with a parameter which will assign the value to the input.
$(".check").click(function() {
if ($(this).is(":checked")) {
$('.add2').val($('.add1').val());
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type=checkbox checked class="check">
<input type="text" id="first" class="add1" />
<input type="text" id="second" class="add2" />
Works when any of one field data changed
let swt_data = "";
$(".add1").on("change", function() {
swt_data = $(this).val();
})
$(".check").click(function() {
if ($(this).is(":checked")) {
if (swt_data != $('#first').val() != $('#second').val()) {
$('#first').val(swt_data);
$('#second').val(swt_data)
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type=checkbox checked class="check">
<input type="text" id="first" class="add1" />
<input type="text" id="second" class="add1" />

jquery increment input values

Using Jquery I need to add all values of input.
Using jquery each is there any way to use regix in element name
like
items[regix_goes_here][debit]
I need values of all input and I want them incremented using each.
My code
<input name="items[1][debit]" type="number">
<input name="items[341][debit]" type="number">
<input name="items[31][debit]" type="number">
<input name="items[431][debit]" type="number">
First, assign a class to your inputs, for example:
<input class="js-inc" name="items[1][debit]" type="number">
<input class="js-inc" name="items[341][debit]" type="number">
<input class="js-inc" name="items[31][debit]" type="number">
<input class="js-inc" name="items[431][debit]" type="number">
Now you can increment all of them, without even jquery. Use .value to access the value as a string. Use Number("..") to convert it to a number. Add one to it, and assign it back.
var inputs = document.querySelectorAll("input.js-inc")
inputs.forEach(function(input) {
input.value = Number(input.value) + 1
})
Here's a live example
If you want to sum all the values of inputs with name="items[X][Y]", you can filter them by testing if their name attribute matches this /items\[\d+\]\[debit\]/ Regex.
This is how should be your code:
var sum = 0;
$('input[type="button"]').click(function() {
$('input').each(function() {
if ($(this).attr('name') && $(this).attr('name').match(/items\[\d+\]\[debit\]/)) {
sum += $(this).val() ? parseInt($(this).val()) : 0;
}
});
console.log(sum);
});
Demo:
var sum = 0;
$('input[type="button"]').click(function() {
$('input').each(function() {
if ($(this).attr('name') && $(this).attr('name').match(/items\[\d+\]\[debit\]/)) {
sum += $(this).val() ? parseInt($(this).val()) : 0;
}
});
console.log(sum);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="items[1][debit]" type="number" value="2">
<input name="items[341][debit]" type="number" value="2">
<input name="items[31][debit]" type="number" value="2">
<input name="items[431][debit]" type="number" value="2">
<input type="button" value="SUM" />

get element id on form change

I'm having a form looks like this:
<form method="POST">
<input type="text" name="htdate" id="htdate">
<br/>
<input type="text" name="pgdate" name="pgdate">
<br/>
<input type="submit" name="submit" value="submit">
</form>
I would like get the textbox id when on form change. My js code looks like this:
$('form :input').change(function()
{
var eleID = $(this).id;
console.log("changeID: ", eleID);
});
Console output :
changeID: undefined
Is there any ways to get the id of the element during value change.?
id is the property of dom object and jQuery object doesn't have any id property, so it will always return undefined
Use this.id or $(this).attr('id') to get id of the element
$('form :input').change(function() {
var eleID = this.id;
// var eleID = $(this).attr('id');
console.log("changeID: ", eleID);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form method="POST">
<input type="text" name="htdate" id="htdate">
<br/>
<input type="text" name="pgdate" name="pgdate">
<br/>
<input type="submit" name="submit" value="submit">
</form>
If you want to listen inputting value then use keypress , keyup or input event
$('form :input').on('input', function() {
var eleID = this.id;
// var eleID = $(this).attr('id');
console.log("changeID: ", eleID);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form method="POST">
<input type="text" name="htdate" id="htdate">
<br/>
<input type="text" name="pgdate" name="pgdate">
<br/>
<input type="submit" name="submit" value="submit">
</form>
use $().attr();
$('form :input').change(function()
{
var eleID = $(this).attr("id");
console.log("changeID: ", eleID);
});
Is there any ways to get the id of the element during value change.?
For this you can use input event which is introduced in the html5. so this can be done:
$('form :input').on('input', function()
{
var eleID = this.id; // <----this.id; || $(this).attr('id');
console.log("changeID: ", eleID);
});
change event only gets invoked when you loose focus from the input so if you are looking for immediate effect while typing/cut-copy-paste then input would be beneficial for you.

Passing value from one field to another

I want to pass the value 9112232453 of one textfield to another.
I know I need Javascript for this but I don't know how to do it.
HTML
<form method = "post" action="">
<input type="checkbox" name="phone" value="9112232453" onclick='some_func();' >
<input type="text" name="phone" value="" id="phone">
<input type="submit" name="Go">
</form>
Then later, I want to use the value in my php.
You could use a JS. function to take param (this.value) like:
<script>
var some_func = function (val){
var input = document.getElementById("phone");
input.value = val;
}
</script>
<form method = "post" action="">
<input type="checkbox" name="phone" value="9112232453" onclick='some_func(this.value);' >
<input type="text" name="phone" value="" id="phone">
<input type="submit" name="Go">
</form>
The best way is to not obtrude the HTML code with Javascript event handlers.
So, you can add a DOMContentLoaded event listener to the document, and as soon as DOM is loaded:
You add a change event listener to the input[type=checkbox], and then:
1.1. If the checkbox is checked, then you change the input#phone's value to its value
1.2. If not, then you empty the input#phone's value.
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('cbphone').addEventListener('change', function(e) {
var phone = document.getElementById('phone');
if (this.checked) {
phone.value = this.value;
// you can even enable/disable the input#phone field, if you want to, e.g:
// phone.disabled = false;
}
else {
phone.value = '';
// phone.disabled = true;
}
});
});
<form method="post" action="">
<input type="checkbox" name="cbphone" id="cbphone" value="9112232453">
<input type="text" name="phone" id="phone">
<input type="submit" name="Go" value="Go">
</form>
before submit form use validation and check whether the field value is filled up or not. if yes get value of the field.
if(document.getElementBy("fieldIdfirst").value!="")
{
document.getElementBy("fieldIdSecond").value=document.getElementElementById("fieldIdfirst");
}
Thanks it..
Try this: http://jsfiddle.net/yhuxy4e1/
HTML:
<form method = "post" action="">
<input type="checkbox" name="phone" value="9112232453" onclick='some_func();' id="chk_phone">
<input type="text" name="phone" value="" id="txt_phone">
<input type="submit" name="Go">
</form>
JavaScript:
some_func = function() {
var checkBox = document.getElementById('chk_phone');
var textBox = document.getElementById('txt_phone');
textBox.value = checkBox.value;
}

Getting the number of unfilled empty text boxes in jQuery

I was trying to solve another person's jQuery question and ran into an issue of my own.
In order to solve this question, I need to determine the number of text boxes that are empty. I thought the best solution would be to use the element[attribute='value'] selector but that didn't work.
alert($("input[val='']").length);
I always get 0, even when there are 3 other empty text boxes. Empty text boxes should have a value equal to an empty string.
Here is my fiddle
http://jsfiddle.net/04gqaLog/
HTML
<input type="text" /></br>
<input type="text" /></br>
<input type="text" /></br>
<input type="text" />
jQuery
$(document).ready(function() {
var sum = 0;
var boxesFilled = 0;
$("input").on("change", function() {
sum += +$(this).val();
boxesFilled += 1;
alert($("input[val='']").length);
});
});
The first issue is you are referring to the value attribute as val. Those are considered two distinct attributes.
The next issue is some input elements may not have a value attribute. Therefore, you will need to specifically check for that or check if the value is falsey
var emptyInputs = $('input').filter(function() {
return !$(this).val();
});
console.log(emptyInputs.length);
$(document).ready(function() {
var sum = 0;
var boxesFilled = 0;
$("input").on("change", function() {
sum += +$(this).val();
boxesFilled += 1;
var emptyInputs = $('input').filter(function() {
return !$(this).val();
});
console.log(emptyInputs.length);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" />
</br>
<input type="text" />
</br>
<input type="text" />
</br>
<input type="text" />

Categories