Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery add variable in img src

Tags:

jquery

I have some images created with php and i want to put each of them inside a li. Here is my working code :

        for ( i = 0; i < MaxNum; i++) {
            $('li#' + i + '').html('<img src="http://www.address.com/somephp.php?Num=1" />')
        }

All i want to do now is put the i variable in the the image source in order to have something like this:

            for ( i = 0; i < MaxNum; i++) {
            $('li#' + i + '').html('<img src="http://www.address.com/somephp.php?Num="+i />')

This code is not working. Is it possible to that with this way?

like image 645
man_or_astroman Avatar asked Aug 28 '26 00:08

man_or_astroman


2 Answers

for ( i = 0; i < MaxNum; i++) {
 $('li#' + i).html('<img src="http://www.address.com/somephp.php?Num='+ i +'" />')
}

Problem to you code

'<img src="http://www.address.com/somephp.php?Num="+i />' in this code i is treated as String, not a variable.

Not a problem but better

to remove + '' part from $('li#' + i + '').

like image 126
thecodeparadox Avatar answered Aug 30 '26 15:08

thecodeparadox


The i is still inside your string, you need something like this:

for ( i = 0; i < MaxNum; i++) {
  $('li#' + i + '').html('<img src="http://www.address.com/somephp.php?Num='+i+'" />');
}
like image 31
Matthew Riches Avatar answered Aug 30 '26 16:08

Matthew Riches