How to assign null value to Hidden variable in javascript? - javascript

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('');
}

Related

how to make input null after submiting?

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 = '';

assign the value of `input ` to a variable but return blank in JS

I want to assign the value of input to a variable in Javascript, but I am not sure why the value of input is always blank.
Here is an example:
let input = document.getElementById('input').value
let val = 'not'
function check() {
//console.log(document.getElementById('input').value) will work
console.log(typeof input)
console.log(input)
}
<input id='input' type='text'>
<button onclick='check()'>Check</button>
The input value is blank (nothing), but the typeof input is string.
If I use document.getElementById('input').value, this will totally work which will display the value of input sucessfully.
Could anyone explain me a little bit why I assign it to a variable won't work?
Thanks for any responds!
Variable assignment val = 'not' takes place at the first time page load. When you need to assign dynamic value, like on a button click, you need an event handling mechanism (In your case, a click event). You are calling a function upon click, but not assigning any value to your variable there. Make assignment inside function like:
function check() {
val = document.getElementById('input').value
console.log(val)
}

Adding "values" to a texfield in jquery/js

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);

Generic method to display error for a not null input box

Desired Result
I'm attempting to create a generic method in Javascript (JQUERY) which checks the page for any and every element whether it is nullable or not.
As a example I'm using a login.
Both the username and password are not nullable. In my head I think it may be possible to have a error label, which is standard hidden, with a for='<inputbox name here>'. Then I can loop through all labels which have the class that shows that the label belongs to a input.
Example
<label for="i_1_s_1">Name: </label>
<input type="text" name="i_1_s_1">
<label class="notnullerror" for="i_1_s_1">hidden</label>
<label for="i_1_s_2">Password: </label>
<input type="text" name="i_1_s_2">
<label class="notnullerror" for="i_1_s_2">hidden</label>
Problem
I'm unaware what the best practice is for this. Basicly what I want is a
foreach (var element = document.getElementsByClass('notnullerror')
{
document.getElementsByName(element.for.value);
}
However, this is done in javascript and not jquery and ofcourse, this will not work. How can I manage to make this the way I want?
And, if this is not best practice for showing a error message on a not nullable input, please comment on what is.
This will work: it finds all notnullerror elements and gets the element that corresponds to the for attribute for each of them.
$(function() {
$('.notnullerror').each(function() {
var forEl= $('input[name="' + $(this).attr('for') + '"]');
//do something here
});
});
It is more common to validate form controls when the form is submitted, rather than running individual tests. You can loop over the controls and validate each based on an attribute value, e.g. if it has a class of notnullerror, test it and display an appropriate error message if necessary.
e.g. (assuming you have a hasClassName function or similar):
var element, elements = form.elements;
for (var i=0, iLen=elements.length; i<iLen; i++) {
element = elements[i];
if (hasClassName(element, 'notnullerror') && element.value == '') {
// deal with error
}
// perform other validation tests
}
Incidentally, the value of a form control is always a string, and therefore can never be == or === to null, though it might be equal to the string "null".

JS: check whether default value have been altered

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.

Categories

Resources