I have the following html table which contains two buttons. Both buttons are handled by the same click handler.
<div id="replaceme">
</div>
<table>
<tr>
<input name="edit" class="edit button" type="button" value="edit" />
</tr>
<tr>
<input name="edit" class="edit button" type="button" value="edit" />
</tr>
</table>
When i click one of those buttons, the following javascript gets executed:
$(document).on('click', '.edit', function (event) {
document.getElementById("replaceme").innerHTML +="textshouldbedynamic <br/>";
});
This works, and the text in 'replaceme' div is replaced properly.
Question: I want do this in a table which is created via asp.net mvc while iterating on a list which is part of my model. I now like to put parts of my model into the javascript function. pseudo code:
<tr>
<input name="edit" class="edit button" type="button" value="edit" data="foo"/>
</tr>
<tr>
<input name="edit" class="edit button" type="button" value="edit" data="bar"/>
</tr>
How can i get such data dynamically into a javascript function?
edit: Is it a correct solution to add a json string, and parse it in the javascript function?
<div id="replaceme">
</div>
<table>
<tr>
<input name="edit" class="edit button" type="button" value="edit" id='{"FirstValue":11,"SecondValue":42}' />
</tr>
<tr>
<input name="edit" class="edit button" type="button" value="edit" id='{"FirstValue":22,"SecondValue":33}'/>
</tr>
</table>
$(document).on('click', '.edit', function (event) {
document.getElementById("replaceme").innerHTML +="textshouldbedynamic <br/>";
var obj = jQuery.parseJSON( this.id );
document.getElementById("replaceme").innerHTML += obj.FirstValue + " " + obj.SecondValue + "<br/>";
});