Dynamic path for image in JavaScript [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 2 years ago.
Improve this question
I'm trying to make a simple code to implement a dynamic source for a image using JavaScript, but without success.
Coding the image path manually it works and the page shows the image.
<script>
function myFunction() {
document.getElementById("myImg").src = "data/3566.JPG";
}
</script>
But when using a variable for dynamic path it shows a X instead of the image.
<script>
let ID = 3566;
function myFunction() {
document.getElementById("myImg").src = "data/ " + ID + ".JPG";
}
</script>
I appreciate any help for checking what I'm doing wrong.
Thanks in advance.

u just have space problem
document.getElementById("myImg").src = "data/" + ID + ".JPG";

Related

Javascript string vs Variable value difference [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 2 years ago.
Improve this question
I wanted to compare the value of variable which is getting the URL of current page with the hard coded string. Though the values are same I am getting false in the comparison. Not able to guess what is happening. Below is the code:
var loc = String('"' + window.URL + '"');
if("Here I am specifying entire URL manually exactly same which I am getting in loc"==String(loc))
{ console.log("true") }
else { console.log("false") }
This is pretty simple but I am missing something. Help needed. Thanks in advance.
You should use window.location.href instead . Since loc variable returns "function URL() { [native code] }" Hence the result is false.
window.location.href === "<Your string>"

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

$(...).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();

document.getElementById() not getting a 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 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";

why i can't get alert info in js? [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 9 years ago.
Improve this question
<script>
var currentNumber = 0;
while (currentNumber <= 12) {
alert(currentNumber);
currentNumber = currentNumber + 2;
}
<\script>
I want to prints out all even numbers from 0 to 12 , Why i can get nothing when run it?
It's possible that your ending <script> tag is causing the problem. The slash is pointing the wrong way. It should be </script>, not <\script>.
Try this code
<script>
var currentNumber = 0;
while (currentNumber <= 12) {
alert(currentNumber);
currentNumber = currentNumber + 2;
}
</script>
You're getting a syntax error because the <script> tag is not closed properly. It isn't <\script>, it's </script>. This same pattern applies to all tags, make sure you have the correct slash.
Maybe you can use a text editor that can highlight mistakes like this.
I am using SublimeText and you may have a try.

Categories