is it possible to check whether default value (set using value="abcdef") of a field with id="someidset" have changed without having info about this default value? Hope it's kind of clear...
When you update the content of an element, the value property changes. However, the value attribute does not. This means that, presuming the value was defined in the value attribute in the original HTML, you can compare the two to see if the one has changed:
var el = document.getElementById('someidset');
if (el.value != el.getAttribute('value')) {
// value has changed
}
Note that this will only reliably work with type="text" inputs.
Well there are attributes and properties.
var someInput = document.getElementById('someInput');
someInput.value; // inputs value right now
someInput.getAttribute('value'); // inputs value set at start
Try this demo: http://jsfiddle.net/maniator/wVazC/
change the value right after the alert and wait 10 seconds
Sure, you can use the defaultValue property. It should work for most types of <input /> elements. Just check it against the value property.
Here's an example.
Related
hello I made a to do list app and I want to clear input after submit here is my code but it doesn't workenter image description here
I expect input section to be null after I submit but every time I have to use backspace then write a new task
First, I would change class="myinput" to id="myinput".
You are assigning user_input to the value of the input at that moment.
Replace your code line to get value by id:
let user_input=document.getElementById("myinput");
let user_input_value=user_input.value;
compare: if(user_input_value!='')
clear with: user_input.value='';
Hi
You just need to save the input element in a variable and set the value property to empty string rather than directly setting user_input = ''.
Also, unless there many inputs you need to loop through, it's better to use id and document.getElementById to identify the input you want rather than document.querySelectorAll
Save the input element as const
const user_input = document.getElementById('myInputId');
// get the value and use as needed
let user_input_value = user_input.value;
After, when you need to reset, set the input elements value to ''
user_input.value = '';
I have a checkbox on my webpage. Based on if the checkbox is checked the variable abc_test needs to be changed either to "wfOutput" or "_blank".
On initial load the variable has the value of "wfOutput" but for some reason the formTarget does not react to the changes based on the variable value. If I change the variable manually everything works and the formTarget receives the correct value.
<input id='runwindow' type='checkbox'>
var abc_test = "wfOutput";
$("#runwindow").change(function() {
if($(this).is(":checked")) {
abc_test = "_blank";
return;
}
abc_test = "wfOutput";
});
The abc_test variable is used in the following code example.
var ap = $("<div>").autoprompt(
{
wfdInfo:xmlInfo,
formTarget:abc_test,
}).autoprompt("instance");
Any ideas what I am doing wrong?
but for some reason the formTarget does not react to the changes based on the variable value.
When you do
var ap = $("<div>").autoprompt(
{
wfdInfo:xmlInfo,
formTarget:abc_test,
}).autoprompt("instance");
the value of abc_test is read and assigned to the formTarget property of the object created by the initializer. There is no ongoing connection between the object property and the variable afterward; changing abc_test later has no effect on the object property.
You'll need to call autoprompt to update the formTarget option from your change handler. Most plugins offer some kind of "update" method.
When writing a new email, I've got a modal(pop-up window in boostrap) that shows a list of contacts. When I select (through checkboxes) a couple of contacts, the selected ones are written into a checkbox. Problem is I'm just writing the lastone I select instead of all of the selected ones.
If you need further explanation please ask. (Sorry for my english)
$("#tblContacto").on("click", ".ck", function(event){
if($(".ck").is(':checked')) {
selected_index = parseInt($(this).attr("alt").replace("Check", ""));
var contacto = JSON.parse(tbContactos[selected_index]);
$("#txtDestinatarios").val(contacto.Email);
} else {
$("#txtDestinatarios").val("");
}
});
Assuming that you want to add all E-Mails into a textfield with id txtDestinatariosthe cause of your Problem is the usage of the $("#txtDestinatarios").val(); function.
Calling val() with an argument sets (and thus overwrites) the value within the textfield. (See demo at http://api.jquery.com/val/#val2)
You would have to first retrieve the value of the textfield using code like var currentValue = $("#txtDestinatarios").val() and then add/remove the E-Mail from/to the string before setting the resulting string back as the value.
If you want to set all selected items in the checkboxes into Textfiled you can use the following line of code :-
$("#txtDestinatarios").val( $("#txtDestinatarios").val()+ ","+contacto.Email);
How can I check for empty values of (required) input fields within a section, and then add a class to them on an event, using jQuery? So far, I have tried:
jQuery("#sender_container input.required").val("").addClass("error");
But that seems to SET the value, rather than checking it. Any ideas?
jQuery("#sender_container input.required").filter(function() {
return !this.value;
}).addClass("error");
Why you have to use filter and not [value=""] you can see in this DEMO
The reason is: attribute selectors check the initial state of the element, not the current state. (note that you can change the "initial" state with the attr function, but it's bad practice, you should always use prop)
So if you change the input value, the current value won't effect the attribute selector. not wise... :)
Notes:
.val() returns the value of the form element, and breaks the jQuery chain,
$('selector').val().addClass('foo') Error, the return value is a string\ number
.val(valueToSet) sets the value of the form element and doesn't break the jQuery chain.
$('selector').val("some value").addClass('foo') - Valid, the returned value is a jQuery
$('input:text[value=]','#sender_container').addClass('error');
DEMO
$('#sender_container input.required[value=""]').addClass('error')
jQuery('#sender_container input.required[value=""]').addClass("error");
You can try this:
$('input:not([value!=""])').addClass('error');
DEMO
Note: This answer should not be used, and the only reason it wasn't deleted is so it can be learned from.
$field = $("#sender_container input.required");
if( ! $field.val())
{
$field.addClass("error");
}
this simple way may work.
If you only need to select based on the initial attribute value of the input then the following will do:
var elements = $('#sender_container input.required[value=""]')
But be aware that this won't work if the value attribute isn't present. It also won't work for the current input value if it has been changed by user or script.
If you'd like to get the current input value you can use jquery's filter function:
var elements = $('#sender_container input.required').filter(function() {
return this.value === '';
// alternatively for "no value":
// return !this.value;
})
After you've selected the jquery elements you can add your class:
elements.addClass('error');
to get all fields inspected this might help.
$('#sender_container [required]').each(function(index)
{
if (!($(this).val())) $(this).addClass('error');
}
});
I have a hidden input variable called str.
I am assigning "abc" value to it.
Then I try to assign null value or let's say null reference to it. But I couldn't.
Edit
part of code.
Hidden Field...
<input id="str" name="str" type="hidden" value="" />
I also use jQuery.
if ($(str).val() == "abc") {
$("#str").val(null);
}
I'm not sure nulling the value is meaningful - you should either blank the value, or delete the whole field (not just the value).
Based on the example code you provided...
For example:
$("#str").val('')
or
$("#str").remove()
Another option, if you may need to toggle the field on or off (so rather than deleting & re-creating) would be disabling the field - disabled fields don't get submitted with the form.
$("#str").attr('disabled','disabled')
and
$("#str").removeAttr('disabled')
Assign the empty string to it. It will be treated the same way on the server side.
var inp = document.getElementById('str');
inp.value = ''; // actually inp.value = null will work here
Or using jQuery
if ($(str).val() == "abc")
{
$("#str").val('');
}