What is wrong with this jquery code, its driving my crazy? [closed] - javascript

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.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I have a div with an id textarea_feedback and a textarea with an id msg.
This is my jQuery code:
$(document).ready(function(){
var text_max = 160;
$('#textarea_feedback').html(text_max + ' characters remaining');
$('#msg').keyup(function(){
var text_length = $('#msg').val().length;
var text_remaining = text_max - text_length;
$('#textarea_feeeback').html(text_remaining + ' characters remaining');
});
});

$('#textarea_feeeback') is a typo.
This is actually a very concrete example of the power of Vanilla JS over jQuery, and of jQuery being too clever for its own good. The Vanilla JS code:
document.getElementById('textarea_feeeback').innerHTML = text_remaining+" characters remaining";
... would have thrown an error saying that it can't set innerHTML of null, which is absolutely correct because textarea_feeeback does not exist. jQuery, however, simply has an object with no elements, and setting the html() of those zero elements is really easy: do nothing.
Unfortunately, "do nothing" is the reason why you can't debug it!

$('#textarea_feeeback').html(text_remaining + ' characters remaining');
^--- typo

Related

JavaScript For-Loop Question: Why Am I getting a syntax error? [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 3 years ago.
Improve this question
I am working through Javascript for Kids, which is a surprisingly fun little book for a beginner, but I'm having trouble testing a For Loop example in the text:
var sheepCounted = 0;
for (var sheepCounted = 0; sheepCounted < 10; sheepCounted++) {
console.log(“I have counted” + sheepCounted + “sheep!”);
}
console.log(sheepCounted);
When I use a Javascript tester to test this example I get a syntax error, but I'm not sure why? Can anyone help me out? Thanks.
Change this line which uses “ ”:
console.log(“I have counted” + sheepCounted + “sheep!”);
To this with " ":
console.log("I have counted" + sheepCounted + "sheep!");

How should this querystring value be formatted in ajax input [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 5 years ago.
Improve this question
I am modifying a code example that I will use (and not very familiar with Javascript)
This is the piece of code
function chk() { cnt++;
var resp=ajax('chk.php', 'POST', 'ordernr='XXXXXX'&r='+((new Date()).getTime()));
if (resp=='3') {
I am posting to this file withe a Querystring-varaible named ordenr
What is the correct syntax to enter the value of Querystring.ordernr instead of XXXXXX
var orderNumber = 1;
var requestParamsStr = 'ordernr=\'' + orderNumber + '\'&r=' + ((new Date()).getTime());
console.log(requestParamsStr);
function chk() {
//cnt++;
var resp = ajax('chk.php', 'POST', encodeURIComponent(requestParamsStr));
//rest of code
}
I think I understand your question. If you need to put quotes around the order number then you must use the escape character / the way I'm doing in the code snippet, so that it doesn't delimit the string (or, alternatively, you could use double quotes within the single quotes). You must also use + to concatenate the the strings.
UPDATE:
I've encoded the request param string as per #ADyson's comment.

Javascript eval in function [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 want to make a function that has to generate a random number between 2 numbers and you can save it in a variable. I'm using this code:
function rand(ran1, ran2, randVar) {
var randomNumb = Math.floor(Math.random()*(ran2 - ran1)) + ran1;
eval("var " +randVar+ " = "+randomNumb+";");
}
rand(12, 49, rand1)
alert("Your number is: "+rand1)
The error I get is: Can't find variable: rand1
Can anyone help me?
Using eval for this is entirely unnecessary. I'd recommend something like this instead:
function rand(ran1, ran2) {
var randomNumb = Math.floor(Math.random()*(ran2 - ran1)) + ran1;
return randomNumb;
}
var rand1 = rand(12, 49);
alert("Your number is: " + rand1);
Notice that in calling the function, the only real difference is in where you place the identifier, rand1.
Further Reading
Functions
return

Why doesn't .attr("src", "/some/path/image.png") 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 8 years ago.
Improve this question
I'm trying to replace the src of an tag with another path. This works perfectly well:
var tempDocId = 'someId';
$('#documents' + that.ticketId).append('<img id="'+tempDocId+'" src="/img/support/pdf_icon.jpg">');
but the following code gives one of those "image not found" icons ():
var tempDocId = 'someId';
$('#documents' + that.ticketId).append('<img id="'+tempDocId+'" src="/img/support/loading.jpg">');
$('#tempDocId').attr("src", "/img/support/pdf_icon.jpg");
Does anybody know what I'm doing wrong here? All tips are welcome!
because you are looking for id="tempDocId", not the one you generated.
$('#tempDocId').attr("src", "/img/support/pdf_icon.jpg");
needs to be
$('#' + tempDocId).attr("src", "/img/support/pdf_icon.jpg");
so you are not replacing the source. My guess is that your loading image is not valid.
Because $('#tempDocId') doesn't exist. tempDocId is a variable, so try:
$('#' + tempDocId).attr('src', '/img/support/pdf_icon.jpg');
var tempDocId = 'doc' + Math.random().toString().substr(2);
var $img = $('<img id="'+tempDocId+'" src="/img/support/loading.jpg">');
$img.appendTo('#documents' + that.ticketId);
$img.attr('src', '/img/support/pdf_icon.jpg');

conditional on value greater than [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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
How would I write a conditional jquery statement saying value greater than?
This is what I have but i don't think it is correct:
} else if ($('.store_checkbox:checked').val > 63){
$("#hidden_taxon_message").show();
the .val > 63 is what I am having a problem with
Try this:-
} else if ($('.store_checkbox:checked').val() > 63){
$("#hidden_taxon_message").show();
val is a function in JQuery so you have to use ().
The jQuery method is .val() you forgot ()
Try this:
$('.store_checkbox:checked').val() > 63
You have forgot () after val and insert the value inside parseInt to tell to javascrip that is a number not a string
try this:
} else if (parseInt($('.store_checkbox:checked').val()) > 63){
$("#hidden_taxon_message").show();

Categories

Resources