Gaurav
Gaurav

Reputation: 535

Passing a string in onclick function's in for loop

I need to pass string as ID in onclick function. When I pass integer as id, it's working fine but when I pass string, it's not even going to the function.

HTML Code ( below is table row. This is inside a for loop)

<td class="col-md-2" width="50" style="background-color: transparent;">
<input class="btn btn-sm btn-primary fa" id='[email protected]' type="button" value="&#xf040" onclick='showedit(@item.ID);' />
</td>

Javascript Code:

function showedit(par) {
        if (editItem == par) {
            return;
        }
        if (editItem > 0) {
            cancel(editItem);
        }

       --code processing--
        editItem = par;
    }

The above code works fine if there is integer value in item.Id. But when I change the HTML code to have string value in item.Id, nothing happens. It isn't even going to the function (checked by putting breakpoint).

HTML Code- with item.ID as string ***the below isn't working***

<td class="col-md-2" width="50" style="background-color: transparent;">
<input class="btn btn-sm btn-primary fa" id='iedit-\"" [email protected] +"\"' type="button" value="&#xf040" onclick='showedit(\"" [email protected]+ "\");' />
</td>

I searched and tried couple of similar options but nothing seem to be working for me. Please advise.

Upvotes: 0

Views: 477

Answers (1)

lukbl
lukbl

Reputation: 1773

Are you using asp.net mvc on server?

 <td class="col-md-2" width="50" style="background-color: transparent;">
<input class="btn btn-sm btn-primary fa" id='[email protected]' type="button" value="&#xf040"  onclick='showedit("@(item.ID)");' />
</td>

Also notice that id of an input will be different than item.ID which you are passing to function - it is prefixed with "iedit" - make sure that does not cause any problems in your function.

Upvotes: 3

Related Questions