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!
Related
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
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 using some simple javascript as below, but for some reasn the catParam is failing with error missing : after id. please help.
var catParam = "(id=cat00000)";
var inputParams = {serviceID:"getCategories",apiKey="asdfasfgx6",catCriterior:catParam};
Use
var catParam = "(id=cat00000)";
var inputParams = {serviceID:"getCategories",apiKey : "asdfasfgx6",catCriterior:catParam};
Instead - you are using an = instead of a : in your object literal. You assign properties of objects in literals using :.
Check out more info here.
Future reference
Try JsHint or JsLint to verify your code!
Also, if you have clean and organized code, it can make it easier to spot small errors like this, as well as improve the error messages you get (as your error will likely be on a shorter line). Using tools like JsBeautifier can get this done easily.
This would be your code after going through JS Beautifier:
var catParam = "(id=cat00000)";
var inputParams = {
serviceID: "getCategories",
apiKey: "asdfasfgx6",
catCriterior: catParam
};
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.
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 have a div:
<div id="results"></div>
and in my js:
document.getElementById('results').innerHTML = "foo";
this works correctly however...
I try to store it...
var rslt = document.getElementById('results');
so I can use it more easily. However "rslt" is undefined and in firebug when I mouse over "results" inside the getElementById brackets it doesn't display any info. Like it's a string??
I'm sure this is probably very simple and I just can't see it...
When I call rslt it gives "null" now. But if I remove the "var reslt = " the rest of it "document.getElementById('results')" works perfectly and returns the div.
window.onload = function(){
var rslt = document.getElementById('results');
rslt.innerHTML = "This is working.";
};
Well, if it is undefined that means one simple thing - the object hasn't made it to the DOM tree yet.
Make that call after you're sure the div has been written to the document, e.g. after load event.
It is Working Perfectly..Look at the Fiddle
<div id="results"></div>
document.getElementById('results').innerHTML = "foo";
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);
}