how to check if hidden field has no value? [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
i make a hidden field and i want if hidden field has empty value then code run
here is my code:
<input type="hidden" id="itemscounter" name="itemscounter" value=""/>
if ($('#itemcounter').val()==""){
$('#itemscounter').val("1");
var counter=$('#itemscounter').val();
var quantity=$('#quantity').val();
var unitprice=$('#unitprice').val();
var linetotal=quantity*unitprice;
that.parent().find('.linetotal').val(linetotal)
$("#invoicetotalamount").val(+linetotal)
var discount=document.getElementById('discount').value ;
var discountamount= discount/100 * linetotal;
var amount=linetotal-discountamount;
$("#balanceamount").val(+amount);
}

There is a typo, your id is 'itemscounter', you are checking for 'itemcounter', also make sure you have put it within the <script> tags.
if ($('#itemscounter').val()==""){
$('#itemscounter').val("1");
var counter=$('#itemscounter').val();
var quantity=$('#quantity').val();
var unitprice=$('#unitprice').val();
var linetotal=quantity*unitprice;
that.parent().find('.linetotal').val(linetotal)
$("#invoicetotalamount").val(+linetotal)
var discount=document.getElementById('discount').value ;
var discountamount= discount/100 * linetotal;
var amount=linetotal-discountamount;
$("#balanceamount").val(+amount);
}

Related

Set parameter as element id then change its value in loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a code here:
$.each(localStorage, function(key, value){
if (key.indexOf('(cache)') > -1) {
var id = key.replace("(cache)", "");
id = '#' + id;
$(id).val(value);
}
});
My target is to update the value of the specific id but it gives an error of unkown expression (expected id here)
and also here is the image of the error
It's highly recommended to convert id properly (don't use dates and spaces). But if you can't then below is a solution
Working snippet:-
myval = 1;
id= '05/03/2018 15:39:51JxM8tX8K';
$('[id="'+id+'"]').val(myval);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="05/03/2018 15:39:51JxM8tX8K"><br>
<input type="text" id="05/03/2018 15:39:51JxM8tX8KDKHFCK">
Note:-I am not fully sure that it will work for you perfectly or not. Test it and let me know

javascript for loop does not work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm creating a site and i needed to do some js. I'm not that good with it but tought i would figer it out. Not. I created a for loop but it does not run.
function order(user,product){
var index;
for(var i = 0; i<users.lenght; i++){
if(user == users[i]){
index = i;
break;
}
}
var budget = budgets[index];
alert(budget);
}
the creation of the users and budgets arrays are done with php and after checking with alert() it was how it should be.
Can anyone help me please?
lenght is spelt length. The misspelt property does not exist, so it undefined, which is equivalent to 0.

JSON with document.getElementById() [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm a little confused with this json. I can type in json manually without document.getElementById("anID"); and it works fine. I keep getting unterminated string literal. Can someone show me the correct way of combining [multiple] document.getElementById() and sending them through json?
var form = '{"first_name":"'+document.getElementById("first_name").value+'",
"last_name":"'+document.getElementById("last_name").value+'"}';
var form = JSON.parse(form);
alert(form['first_name']);
alert(form.last_name);
This works. I think this is what you are trying to figure out:
var form = {"first_name":document.getElementById("first_name").value,
"last_name":document.getElementById("last_name").value};
console.log(form.first_name);
console.log(JSON.stringify(form));
It occurred to me that this is probably what you really wanted. You need to clean up your quotes, then use a little bit of eval():
var form = {"first_name":"document.getElementById('first_name').value",
"last_name":"document.getElementById('last_name').value"};
var runThatFunction = eval(form.first_name);
console.log(runThatFunction);
You can just build a Javascript object like this -
var show = function() {
var form2 = {};
form2.first_name = document.getElementById("first_name").value;
form2.last_name = document.getElementById("last_name").value;
alert(form2['first_name']);
alert(form2.last_name);
};
<input type="text" id="first_name" value="Eric" />
<input type="text" id="last_name" value="Johnson" />
Click me!

$(...).offest is not a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am baffled to why I am experiencing this error, maybe its a bug with Jquery or maybe I'm blind, but Jquery has loaded and I can access the offset function after selecting an element?
Uncaught TypeError: $(...).offest is not a function
According to w3, I've used the function correctly.
Current code
var off = $("#canvas").offest();
var xPos = off.left + ( jqo.outerWidth(true)/2 );
var yPos = off.top + ( jqo.outerHeight(true)/2 );
console.log(yPos, xPos);
You have typo. Use offset instead of offest:
var off = $("#canvas").offset();

Javascript var inisde array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to use the value of a variable inside a array, like this.
var advalue = Math.floor(Math.random * 6);
var ads = ["Hello", "World", "Randomword", "123", "1233", "0000"];
API.sendChat("Word: " + ads[advalue]);
When i run this, i get undefined
There is a typo, missing brackets after random
var advalue = Math.floor(Math.random() * 6);

Categories