i am using multiple numbers of jquery select2
in my html
<select class="select2_demo_3 form-control" style="color: #1c84c6" id="selectSubject" name="selectSubject">
<option value="">Select a Subject</option>
<?php
$query= "SELECT * FROM `tblsubject` WHERE `status`='1'";
$found=mysqli_query($con,$query);
while($row = mysqli_fetch_array($found)){
?>
<option value="<?php echo($row['id']);?>"><?php echo($row['subject_name']);?></option>
<?php
}
?>
<select class="select2_demo_3 form-control" id="selectTopic" name="selectTopic">
<option value="">Select a Topic</option>
<?php
$query= "SELECT * FROM `tbltopic` WHERE `status`='1'";
$found=mysqli_query($con,$query);
while($row = mysqli_fetch_array($found)){
?>
<option value="<?php echo($row['id']);?>"><?php echo($row['topic_name']);?></option>
<?php
}
?>
and so on then in my script
$(document).ready(function(){
$(".select2_demo_3").select2();
});
the problem is though i am giving it in id tag of each select , on inspecting the element i am getting a different id which is being set by jquery internally i think. screenshot of inspect
as you can see it shows my given id which is "selectSubject" but when i am writing in my script
<script type="text/javascript">
var questionType=document.getElementById("selectSubject").value;
</script>
i am getting
"Uncaught TypeError: Cannot read property 'value' of null" error
what am i doing wrong? can i get select2 value in javascript? or what is the right way to do it?
AS I AM USING MULTIPLE NUMBERS OF SELECT2 I WANT INDIVIDUAL SELECT VALUE BY ID OR SOME OTHER WAY.
edited: corrected the id in getElementById to "selectSubject".