On Mon, 6 Mar 2017 23:30:20 -0800 (PST)
Rejoy <rejo...@gmail.com> wrote:

> I am getting data from the server and iterating over it to create a
> table and then I am using a form to store an id to local storage
> using javascript. Here is code snippet
> 
> <table>
>     <tr><th>Product ID</th></tr>

This loop

>     {{range .}}
>     <td ><form onsubmit="save_data()" action="/" method="get"><button
> class="btn btn-info pid" id="pid" name="{{.puid}}"
> value="{{.puid}}">Update</button></form></td> {{end}}

would create N table rows with N buttons all having the same "id"
attribute set to "pid", so this callback

[...]
> function save_data() {
>   var input = document.getElementByID("pid");
>   localStorage.setItem("id", input.value); 
> }
[...]

will always find the topologically first element by that attribute.
Isn't that your exact problem then?

> However every time, no matter of which table row's "update" button I
> click, everytime only the ID of the first table row element is
> getting stored. Is there a way I can generate unique IDs and
> reference it in Javascript when ranging over the data. Thanks

I'd say, in your loop, you should roll like this:

1) Increase some simple counter variable initially set to, say 0.

2) Use it to generate the values for the "id" attributes of your
   buttons.

3) Set callback in a way to pass it the exact attribute value to search
   for, like with

     form onsubmit="save_data($pid)

4) In the callback, use the supplied value directly.

Or maybe just not reinvent the wheel and use some JS library which
would produce data bindings for you do you don't have to wire explicit
callbacks etc.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to