0

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.

2
  • 1
    Thanks. But I am very new to all this and this is my first project involving JS & HTML. Unfortunately never used jsfiddle. After your comment I am trying to use jsfiddle. Gmme some time to try and post back. Thanks Commented Apr 4, 2016 at 10:37
  • 1
    see @lukbl answer in which quotes are added to the showedit() parameter. Missing quotes in your code seems the mostly likely problem. Commented Apr 4, 2016 at 10:50

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Yes I am using asp.net mvc. This worked. Was struggling for last 3-4 hours. Will check the other point too. Thanks again for everything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.