0

I am calling a javascript function from within my razor code..but the jquery code within my javascript function doesnot get executed..

  • what is the correct way of doing it.

    function getPosition(id) {
      var c = '#' + id;
     return $c.index();
    }
    

My HTML Table

  <tbody>
      <tr>

         <td>
            @foreach(Result geResults in Model.results)
            {
            @:
              <script>
              {
               getPosition(@geResult.assessmentId);
              }
              </script>
             }
         </td>

      </tr>
  <\tbody>

UPDATE

As everybody is getting confused i am posting more detail

<table>
    <thead>
            <tr>
            @foreach (Assessment geAssessment in Model.assessments)
                {
                    <th [email protected]>@geAssessment.Name</th>
                }

        </thead>
        <tbody>
              <tr>
                  @{
                      // add a td for each assessment in body
                    foreach(Assessment geAssessment in Model.assessments)
                    {
                        <td>
                          @foreach (ShortResult geResult in Model.results)
                          {
                             @:
                             <script>
                             {                                                                   getPosition(@geResult.assessmentId);
                              }
                            </script>

                            }
                         </td>  
                                    }
                                 }
              </tr>
         </tbody>

i want to return the column index in getPosition function and then print it in the td..hope this clears out any confusions

currently it says getPosition is out of context whereas intellisense shows me getPosition when i code

5
  • What you are trying to achieve? Commented Mar 10, 2017 at 5:35
  • $c is not defined. You mean $(c). Commented Mar 10, 2017 at 5:37
  • Try this return $('#' + id).index();. also u need to visit this api.jquery.com/index Commented Mar 10, 2017 at 5:38
  • I don't think that is how it works... you might need to instead add an element with an onload to load the function. I don't know razor code so I wouldn't really know but that simply just looks wrong. Try adding in a console.log and just try to get something on the console. Commented Mar 10, 2017 at 5:44
  • What is the value of this @geResult.assessmentId and do you have any element with id of @geResult.assessmentId's value? Commented Mar 10, 2017 at 5:53

2 Answers 2

1

Just put c in brackets. It will be like this:

function getPosition(id) {
  var c = '#' + id;
 return $(c).index();
}

If it doesnt throw

$ Undefind error

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

2 Comments

I have actually defined this id in my td as: <th [email protected]>@geAssessment.Name</th> and then i try to find the right header/column for each result in tbody...it was getting called before and now it says getPosition does not exist in the current context..I have described my code in detail here: stackoverflow.com/questions/42708805/…
this answer seems fine but i am unable to test because of the error see update
-1

first of all check that your passing the values means getPosition("") is not null secondly write in JavaScript

function getPosition($id) {
  var c = '#' + $id +;
 return $(c).index();
}

1 Comment

The change from id to $id was pointless, wrong syntax has been introduced and you didn’t explain the issue with $c.

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.