How to use dynamic golang html template id with javascript? -


hi have html image button dynamic id inside range in golang template. need add javascript function it. problem how use dynamic id in javascript?

my html

{{range $i, $e := .process}}    <input id="id{{.}}" type="image" src="/images/icons/pencil.png" alt="edit" width="15" height="15"> {{end}} 

javascript

<script type="text/javascript">        $().ready(function() {             $('#id{{.}}').click(function() {                 $('#hidebody').toggle();              });         });     </script>    

how solve this? there better way this?

give buttons class.

{{range $i, $e := .process}}    <input id="{{.}}" class="img-buttons" type="image" src="/images/icons/pencil.png" alt="edit" width="15" height="15"> {{end}} 

in javascript can do,

$(".img-buttons").click ( function() {     $(this).attr( "id" ); // id }); 

instead of id can use html data attributes if suits better.

html:

{{range $i, $e := .process}}    <input data-id="{{.}}" class="img-buttons" type="image" src="/images/icons/pencil.png" alt="edit" width="15" height="15"> {{end}} 

js:

$(".img-buttons").click ( function() {     $(this).data( "id" ); // id }); 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -