javascript - Set ID as input value on click of an element -


when click on link temporary pops id number of link.

question how make if click on link id add id input value id input[id="language_id"]

code-preview my codepen example

jquery

$(document).ready(function() {   $(".language li a").click(function(event) {      // test make sure getting id     alert(event.target.id);   }); }); 

html

<div class="container">   <ul class="dropdown-menu language" role="menu">     <li><a href="#" id="1">test 1</a></li>     <li><a href="#" id="2">test 2</a></li>     <li><a href="#" id="3">test 3</a></li>     <br/>     <br/>   </ul>   <form action="" method="post" id="language-form">     <input type="text" name="language" id="language_id" placeholder="displays id" value="" />   </form> </div> 

you have elements same id; invalid html. change 1 of them or use classes.

element.id

it must unique in document

for question use this.id val(),

$('#language_id').val(this.id)   //or event.target.id 

updated codepen

$(".language li a").click(function() {     $('#language_id').val(this.id) }); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -