Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello all

i want to get value of select items in my Javascript code.
this my view code.


HTML
<pre><div class="form-group ">
                                    <label id="locationdepart" class="control-label"> Stockage de depart </label>
                                    <select id="locationdepart" class="form-control" asp-items="ViewBag.fromlocation"> </select>
                                    <span id="locationdepart" class="text-danger"> </span>
                                </div>
                            </div>

                            <div class="col-lg-3">

                                <div class="form-group">
                                    <label id="locationarrive" class="control-label"> Stockage d'arrivée </label>
                                    <select id="locationarrive" class="form-control" asp-items="ViewBag.fromlocation"> </select>
                                    <span id="locationarrive" class="text-danger"></span>
                                </div>


What I have tried:

this is my JS code.

this my error :
Uncaught TypeError: Cannot read property 'value' of null


JavaScript
 function onAddClick() {

            var transfertDep = document.getElementById("#locationdepart").value;
          //  var transfertDep = e.options[e.selectedIndex].value;

            var transfertArr = document.getElementById("#locationarrive").value;
}
Posted
Updated 18-May-20 9:06am

Quote:
HTML
<label id="locationdepart" class="control-label"> Stockage de depart </label>
<select id="locationdepart" class="form-control" asp-items="ViewBag.fromlocation"> </select>
<span id="locationdepart" class="text-danger"> </span>
You're setting the same id on the label, the form element, and the validation message. IDs in an HTML document must be unique.

Since you're using ASP.NET Core, you can use the tag helpers to generate the correct IDs:
HTML
<label asp-for="locationdepart" class="control-label"> Stockage de depart </label>
<select id="locationdepart" asp-for="locationdepart" class="form-control" asp-items="ViewBag.fromlocation"> </select>
<span asp-validation-for="locationdepart" class="text-danger"> </span>
If you view the page source in your browser, the rendered HTML should look something like:
HTML
<label for="locationdepart" class="control-label"> Stockage de depart </label>
<select id="locationdepart" name="locationdepart" class="form-control"">
    <option value="...">...</option>
    ...
</select>
<span class="text-danger field-validation-valid" data-valmsg-for="locationdepart"></span>
Your script should then start working:
JavaScript
function onAddClick() {
    var transfertDep = document.getElementById("#locationdepart").value;
    console.log(transfertDep);
    ...
}
 
Share this answer
 
Comments
Member 13220552 18-May-20 15:19pm    
Thanks you Richard , i have seen , it's my IDs. i have removed for label and validation message.
"value" is for form elements like textbox, combo etc. Labels and spans don't have a value, so use innerHTML or innerText to get the html inside a tag rather than "value".
 
Share this answer
 
Comments
Member 13220552 18-May-20 14:19pm    
thank i try that and i come back

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900