1

Updates:

So right now I changed my code to this:

<script>
        function myFunction(calid) {
            document.getElementById(calid).innerHTML = "<ul><li>" + calid + "</li>" + "</ul>";
        }
        </script>

        {% for calibrations in equipment.calibration_set.all %}


        <ul>
            <li>
                <p  onclick="myFunction( {{calibrations.id}} ) ">   {{calibrations.cal_date}}    </p>
                <div id = {{calibrations.id}}> YES </div>


            </li>
        </ul>

        {% endfor %}

And it works fine. Thanks!

But then there is one more weird problem: when i tried to add more arguments to the function so that it could display additional information, it just stop working which is really strange since only thing I changed is adding a new arguments. So if I try this:

<script>
        function myFunction(calid, calby) {
            document.getElementById(calid).innerHTML = "<ul><li>" + calid + "</li>" + "<li>" + calby + "</li>" + "</ul>";
        }
        </script>

        {% for calibrations in equipment.calibration_set.all %}


        <ul>
            <li>
                <p  onclick="myFunction( {{calibrations.id}}, {{calibrations.cal_by}} ) ">   {{calibrations.cal_date}}    </p>
                <div id = {{calibrations.id}}> YES </div>


            </li>
        </ul>

        {% endfor %}

then when i click the date, nothing happens.

  1. how come this is wrong?
  2. maybe I should use JSON for passing a model data into javascript?

Old post: I will just dump my code here first:

{% for calibrations in equipment.calibration_set.all %}
        <script>
        function myFunction(cal_id) {
            document.getElementById(cal_id).innerHTML = "<ul><li>{{calibrations.id}}</li></ul>";
        }
        </script>
        <ul>
            <li>
                <p  onclick="myFunction( {{calibrations.id}} )">{{calibrations.cal_date}}</p>
                <div id = {{calibrations.id}}> YES </div>


            </li>
        </ul>

so calibration is a foreign key of equipment. And here I would like to display a list of calibration associated to a equipment, and using the date of calibration as a javascript onclick event so that a user can click to display the detail information of this calibration. The problem I have is that now even if there are multiple calibrations, clicking the calibration date will change the "Yes" under it to the id of latest calibration.

[![enter image description here][1]][1]

Like in the picture, I clicked the first and second calibration date and expect the text below it to be 1 and 2 respectively.

Could somebody point out what's wrong here?

[1]: https://i.sstatic.net/kRFkl.png

1 Answer 1

3

You're creating javascript function with same name in every loop. That's why all click are returning same result.

Simple solution is:

function myFunction(cal_id) {
    document.getElementById(cal_id).innerHTML = "<ul><li>" + cal_id + "</li></ul>";
}

But in general, it's not right to create new function (especially with same name) in every loop.

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

1 Comment

Thanks so much! And by the way there is another related new problem that just came out if you happen to have the time to take a look

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.