简体   繁体   中英

on click not working on font awesome icon

I have a simple input with a search icon in its right part. When I click this icon i simply want to trigger an event, say printing something on the console.

So I have the following HTML :

<div class="input-group stylish-input-group">
  <input type="text" class="form-control"  placeholder="Search" >
    <span class="input-group-addon">
      <button id = "btnSrch" type="submit"/>
        <span class="fa fa-search" aria-hidden="true"/>
    </span>
</div>

The icon is in the innermost span tag <span class="fa fa-search" aria-hidden="true"/>

I simply want to log something on the console when I click this icon.

I tried the following jquery :

$('.fa-search').click(function(){
        console.log("dddddd");
    });

also this one :

$("body").on('click', ".fa-search", function(){

            console.log("dddddd");
        });

Can someone help me make this work. I am new to this, but I don't see what am I doing wrong..

EDIT

That HTML structure is invalid. button is a block element as well as span as pointed out by @adam in comments ws the real problem


It should work it is simple and straight just make sure you are calling it on document.ready or before the </body> tag.

 $(document).on('click','.fa-search',function(){ console.log('clicked'); }) 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="input-group stylish-input-group"> <input type="text" class="form-control" placeholder="Search" > <span class="fa fa-search" aria-hidden="true"/> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM