How to use string in place of url in image source [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 6 years ago.
Improve this question
I am trying to embed an image in a div. The source of the image is dynamic which is stored in the attribute 'weatherIconURL' of the JS Object 'storeLocations'. That is for each storeLocation I am trying to show the corresponding weatherIcon image. Below is the code I am using
var block = '<div>'+'<p><b>'+storeLocations[i].accountName+'</b></p>'+
'<p>Min Temp: '+storeLocations[i].minTemp+'</p>'+
'<p>Max Temp: '+storeLocations[i].maxTemp+'</p>'+
'<img src="storeLocations[i].weatherIconURL"/>'+
'</div>';
The image is not getting displayed. I have tried using escape character as well.

You're not including the variable in the string the same way you are all the others. Notice the difference? This should fix you up:
var block = '<div>'+'<p><b>'+storeLocations[i].accountName+'</b></p>'+
'<p>Min Temp: '+storeLocations[i].minTemp+'</p>'+
'<p>Max Temp: '+storeLocations[i].maxTemp+'</p>'+
'<img src="' + storeLocations[i].weatherIconURL + '"/>'+
'</div>';
Syntax highlighting (like that used here on SO) should make this stand out to a simple visual inspection.

why are you using concatinate so much simply add this code
var block = '<div><p><b>'+storeLocations[i].accountName+'</b></p><p>Min Temp: '+storeLocations[i].minTemp+'</p><p>Max Temp: '+storeLocations[i].maxTemp+'</p><img src='+storeLocations[i].weatherIconURL+'/></div>';

Related

Problem adding Js variables in HTML with 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 4 years ago.
Improve this question
<body>
<h1>Operadores aritméticos</h1>
<h3>+ Adición (102+103)=</h3><p id="suma1"></p>
<h3>- Substracción</h3><p id="resta1"></p>
<h3>* Multiplicación</h3><p id="multi1"></p>
<h3>/ División</h3><p id="div1"></p>
<h3>% Módulo</h3><p id="mod1"></p>
<script>
var suma = (102+103);
var resta = (36-20);
var multiplicación = (27*30);
var división = (900/30);
var módulo = (106%3);
document.getElementById("suma1").innerHTML = suma;
document.getElementById("resta1")innerHTML = resta;
document.getElementById("multi1")innerHTML = multiplicación;
document.getElementById("div1")innerHTML = división;
document.getElementById("mod1")innerHTML = módulo;
</script>
Hello guys, I have a problem, im pretty new at this (programming with HTML, Js, etc.).The issue is that when I try to make my Js variables appear on HTML (with document.getElementById), they do not appear. Nevertheless, if erase every document.getElementById except the one containing "suma1", the browser displays me the result of the sum (205), but if I add even one of them, the browser doesn´t display anything.
I hope I was clear with my problem, it seems very simple but hard to explain.
Any suggestions?
Thanks in advance
The reason you're not seeing anything when you add the statements to populate resta1, multi1, div1, and mod1 is probably because they all have a syntax error. This is likely causing even the first statement (suma1, which is syntactically valid) not to work.
Valid Statement
The 1 statement that is working is document.getElementById("suma1").innerHTML = suma;
Invalid statements
All the other statements follow this pattern:
document.getElementById("id")innerHtml = variable;
Note that you're missing the . between getElementById("id") and innerHtml. If you add the missing . then it should all work as expected.

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 backgroundColor works on some computers, not on others? [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
This part of javascript code works on my home computer, but doesn't at work. Also, my colleague tested it and it doesn't work on her home computer?!
function klikNaX(x, y, z) {
var u = document.getElementById(z);
var u2;
u2 = u.style.backgroundColor;
document.getElementById("test").innerHTML = (z + " " + u2);
}
The problem seems to be with this part
u.style.backgroundColor;
Its not a problem of style.backgroundColor, instead please check if 'u' is a valid node and not undefined.
And try setting a color to u.style.backgroundColor = 'green'

Pass jquery variable to php [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 question I tried to pass a jquery variable to php but when I look in console I got an error:
SyntaxError: missing ) after argument list):.append('Edit') and I don't understand where is the problem
var items=[];
var link = "<?php echo base_url()?>firm/editFirm/";
$.each(obj, function(i,val)
{
$('#finalResult').text("Results");
items.push($('<li/>').text
(
val.name_firm + "---" +
val.idno+"---" +
val.adresa+ "---" +
val.cont_banca+ "---" +
val.swit+ "---" +
val.banc_name+"---"
).append("<a href='"link+val.idno"'>Edit</a>")
);
});
Help me please.
You've missed the concatenation in the last bit:
append("<a href='"link+val.idno"'>Edit</a>")
Should be
append("<a href='" + link + val.idno + "'>Edit</a>")
I hope you aren't calling the php inside a .js file. I find it's cleaner to retrieve server-side variables using data-attributes inside DOM elements.
In the case of storing URLs for your use in Javascript, I'm really fond of the localize_script function of Wordpress, you just render the variables in a tag in the header or footer via PHP and you have them all available in a nicely separated place.

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');

Categories

Resources