1

This code describes to select subjects in the course
HTML code is

<form action="mode" onSubmit="calcRoute();return false;" id="keywordForm">
                                            <table border="0">
                                                <thead>
                                                <tr>
                                                    <td>COURSE</td>
                                                    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                                    <td>SUBJECTS</td>
                                                    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                                    <td>TOPIC</td>
                                                </tr>
                                                </thead>
                                                <tr>
                                                    <td>
                                                        <select id="courseName" name="courseName" multiple size="10">
                                                            <c:forEach items="${courses}" var="course">
                                                                <option value="${course.courseId}">${course.courseName}</option>
                                                            </c:forEach>
                                                        </select>
                                                    </td>
                                                    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>

                                                    <td>
                                                        <select id="subjectName" name="subjectName" multiple size="10">
                                                        </select>
                                                    </td>

                                                    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                                    <td>
                                                        <select id="topicName" name="topicName" multiple size="10">
                                                        </select>
                                                    </td>                                                    <!-- /controls -->

                                                    <td>
                                                        <button id="submit"><i class="icon-chevron-right"></i></button>
                                                    </td>

                                                </tr>
                                                <div id="alert"></div>
                                                <!-- /controls -->
                                            </table>
                                            </form>

Ajax code is

$('#courseName').change(function () {
       var courseId = $(this).val().toString().replace("[", "");
//        var courseName = $(this).val().toString().replace("[", "");
        $.ajax({
            type: "POST",
            url: "/sil-web/student/select-subjects?courseId=" + courseId,
            //url: "/sil-web/student/select-subjects?courseName=" + courseName,
            contentType: 'application/json',
            success: function (response) {
                $("#subjectName").children().remove();
                $.each(response, function (index, item) {
                    console.log("subject" + item.subjectId);
                    $("#subjectName").append("<option value=" +  item.subjectId + ">" + item.subjectName + "</option>");
                });
            }
        });
    });

In this tag subject name not fully taken

$("#subjectName").append("<option value=" +  item.subjectId + ">" + item.subjectName + "</option>"

It took like

<option value="advanced" algorithms="">advanced algorithms</option>

because of that I get course name as advanced algorithms as advanced only since it is not display subjects under that course how to solve this?

SamanthaJeyakumar
  • 121
  • 1
  • 2
  • 6

2 Answers2

1

Try this :

$("#subjectName").append("<option value='" +  item.subjectId + "'>" + item.subjectName + "</option>"
Brijesh Bhatt
  • 3,810
  • 3
  • 18
  • 34
1

I think this is related to this post: Escape quotes in JavaScript You have a " in your subjectId. You have to convert it to HTML code by doing this:

item.subjectId.replace(/"/g, '&quot;')
Community
  • 1
  • 1
Sjoerd
  • 1,724
  • 2
  • 13
  • 13