Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send a random generated value to javascript function onclick

I have a scenario where i need to pass randomly generated id to the javascript function, but javascript function is not recognizing. Iam using dojo enhanced grid. Below is the code:

function(value, rowIndex) {
            var Id = this.grid.getItem(rowIndex).ID;
            alert("ID+ " + Id);//retriving the id value correctly
            return  "<img src=../img/smiley.jpg width=\"20\" height=\"20\" onClick=\"showData(\''+Id +'\')\" class=\"display\">"  ;
        };

<script>
function showData(id){
alert(id);//unable to get the id here..
}
</script>

Thanks in advance.

like image 939
participantjava Avatar asked Oct 20 '22 09:10

participantjava


1 Answers

The quotes in the listener are messed up. I tend to use double quotes for HTML and single for script, and nest as appropriate:

return  '<img src="../img/smiley.jpg" width="20" height="20" onclick="showData(\'' + Id + '\');" class="display">';

Also, I much prefer to use lower case for all attribute names and to quote all values, not just those that strictly need it.

Finally, variable names starting with a capital letter are, by convention, reserved for constructors so you should use id not Id.

like image 165
RobG Avatar answered Oct 23 '22 01:10

RobG