Click here to Skip to main content
14,544,882 members
Rate this:
Please Sign up or sign in to vote.
hello all

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


<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


 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
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
<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:
<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:
<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:
function onAddClick() {
    var transfertDep = document.getElementById("#locationdepart").value;
    console.log(transfertDep);
    ...
}
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

"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".
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100