Xin Zhao
Xin Zhao

Reputation: 1

how to make id which is not a unfixed value in $("#") works?

My code as below:

var tElement=document.getElementById("test");

for(i=0;i<num;i++) 
{
  var img=document.createElement('img');
  img.src="img.jpg";
  img.id="'r'+i";
  tElement.appendChild(img);
  Tclick(i);
}

function Tclick(num){

  $("'#r'+num").click(function (num) {
    $("#display").append("<img src='img2.jpg'>");
  });

}
<div id="test"></div>
<div id="display"></div>

however, the application always say '#r'+num is unexpected error, I don't how to write it so that it can work. plz help me. thanks.

Upvotes: 0

Views: 20

Answers (1)

Satpal
Satpal

Reputation: 133423

You are not using variable's properly. Since i and num are variable you don't need to wrap it in quotes.

Use

img.id='r'+i;

instead of

img.id="'r'+i";

And

$('#r'+num)

instead of

$("'#r'+num")

Upvotes: 1

Related Questions