html - How to get a selected list item using Javascript? -


i display list of contents database in application. fetch records database , iterate through for-each loop in front end , displays contents. when try click of items shows first item in list. below code. how can overcome issue?

function userselection()         {             var selecteduser1=document.getelementbyid("lbl_user").innerhtml;             document.writeln(selecteduser1);         }  <div style="border: 2px solid activeborder;width:300px;height: 400px;border-radius: 5px;">                     <ul id="userlist" style="text-align: left;">                         <c:foreach items="${list_onlineusers}" var="userslist">                             <li  id="li_user" style="list-style-image: url(images/online.png);cursor: pointer;height: 25px;margin-left: 0;margin-right: 10%;margin-top: 0.5em;margin-bottom: 7%;" value="${userslist}">                                 <label onmousedown="userselection()" id="lbl_user" style="font-family:trebuchet ms,times,serif;color: black;font-size: 16px;cursor: pointer;">                                     ${userslist}                                 </label>                             </li>                         </c:foreach>                     </ul>                 </div> 

definition , usage of id

the id attribute specifies unique id html element (the value must unique within html document).the id attribute used point style in style sheet, , javascript (via html dom) manipulate element specific id.

you're using same id items in list. have change <c:foreach> loop little add loop count id. so, each item have unique id. please change code below

<c:foreach items="${list_onlineusers}" var="userslist" varstatus="count">    <li  id="li_user${count.index}" value="${userslist}">         <label onmousedown="userselection()" id="lbl_user${count.index}">           ${userslist}          </label>    </li> </c:foreach> 

the above change set unique id each item. can pass unique id of item clicked on registering eventlistener document , whatever want. first of all, need ensure there unique id every element in document.

note : have removed inline css style properties html improve readablity


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -