Set parameter as element id then change its value in loop [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 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

Related

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!

JavaScript match() function does not give output [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
In JavaScript code I use regular expression match() function to find pattern and show the result, but it does not give a proper answer, means finding [i] character in giving sentences
<button onclick = "searchPattern()">pattern</button><br>
<p id = "demo"></p>
<script>
function searchPattern(){
var str = "Visti W3Schools!";
var paat = /[i]/g;
var result = str.match(patt);
document.getElementById("demo").innerHTML = result;
}
</script>
Read your JavaScript console
Uncaught ReferenceError: patt is not defined
You changed your variable name from paat to patt half way through your code.

How to remove value from array? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I wanted to insert value in if condition and remove it in else condition.
$(document).ready(function(){
var ids=[];
$('input[type="checkbox"]').click(function(){
if($(this).prop('checked')){
var id = $(this).val();
ids.push(id)
}
else
{
//remove value from array
}
});
});
Instead of :
array.push(id)
It should be :
ids.push(id)
(because your array name is ids not array)
Click event's callback function parameter is event
$('input[type="checkbox"]').click(function(array){
"array" is instance of event on above code.
Then variable "array" cannot have function "push".

Why does JS throw unexpected error while re-writing a variable? [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
Error is:
function (a){var b,c,d,e=this[0];{if(arguments.length)return d=m.isFunction(a),this.each(function(c){var e;1===this.nodeType&&(e=d?a.call(this,c,m(this).val()):a,null==e?e="":"number"==typeof e?e+="":m.isArray(e)&&(e=m.map(e,function(a){return null==a?"":a+""})),b=m.valHooks[this.type]||m.valHooks[this.nodeName.toLowerCase()],b&&"set"in b&&void 0!==b.set(this,e,"value")||(this.value=e))});if(e)return b=m.valHooks[e.type]||m.valHooks[e.nodeName.toLowerCase()],b&&"get"in b&&void 0!==(c=b.get(e,"value"))?c:(c=e.value,"string"==typeof c?c.replace(lc,""):null==c?"":c)}}
What I am trying to do is to catch a variable on Focus Out:
$('.field').focusout(function() {
var date = $('.field').val;
console.log(date);
//Do something else
});
The HTML for field is:
<input type="text" class="field" />
Just a Typo...
val is a function. Use val()
Yes I know this question is not worth an answer, but otherwise someone will just transplant the comment as their own answer :)

how to check if hidden field has no value? [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 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);
}

Categories