views:

684

answers:

4

hi ,

i am trying to add a label from javascript but im getting error

what i did was:

document.getElementById(option).innerHTML="<label onclick='updateInfo('edit','qualification',"+data_id+","+data+")>"+data+"</label>";

what am i doing wrong ?

+5  A: 

I think the error is with the quotes inside the function. You have to escape the quotes.

document.getElementById('option').innerHTML="<label  onclick=\"updateInfo('edit','qualification','"+2+"','"+test+"')\">"+data+"</label>";
rahul
+1  A: 
David Dorward
A: 

The quotes are wrong. you've used single-quotes to quote the onclick value and it's contents.

onclick='updateInfo('edit','qualification',"
SpliFF
A: 

Another way is to use document.createElement() and document.appendChild() to avoid the escaping. It also makes the code more readable.

        var option = document.getElementById("option_id");
     var label = document.createElement("label");
     label.onclick = function() {
      updateInfo('edit','qualificn',"'" + data_id + "'", "'" + data + "'")
     }
     label.innerHTML = data;
     option.appendChild(label);
letronje