Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set html id element as variable in javascript

Tags:

javascript

I saw in the firebug the ID is like this whereas I want the value of existingProductArray[i] to be the ID. What am I doing wrong?

var html ='<li id="existingProductArray[i]">'
       + '<input type="image" id="existingProductArray[i]" src="images/subtract.png" onclick="javascript:deleteProduct(this.id)" href="#">'
       + existingProductArray[i]
       + '</li>';
     
like image 635
Saurabh Kumar Avatar asked Dec 22 '22 10:12

Saurabh Kumar


1 Answers

Try this

 var id = existingProductArray[i];

 var html ='<li id="' + id + '">'
           + '<input type="image" id="' + id + '" src="images/subtract.png" onclick="javascript:deleteProduct(this.id)" href="#">'
           + id
           + '</li>';

But

ID hence the name should be unique you are giving 2 elements the same ID ( that's a bad idea )

like image 156
iConnor Avatar answered Dec 24 '22 00:12

iConnor