s k
s k

Reputation: 5202

How to add custom button to Bootstrap Input

The closest I can find is:

input[data-icon] {
  padding-left: 25px;
  background: url("https://static.thenounproject.com/png/101791-200.png") no-repeat left;
  background-size: 20px;
} 

Search Button

But this icon will be removed once the JQuery's validation icon appears.

I knew Bootstrap input group can prepend and append icons, but those icons had already been reserved for other usage (in my project).

Besides, I wish the added icon is clickable as well. Added as a background image doesn't work.

Upvotes: 0

Views: 63

Answers (1)

Rok Benko
Rok Benko

Reputation: 22930

Here you go...

This is exactly what you want. The icon is also clickable. See the snippet below.

function myFunction() {
  alert("I am clickable!");
}
.input-icon:hover {
  cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">

<head>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-skAcpIdS7UcVUC05LJ9Dxay8AXcDYfBJqt1CJ85S/CFujBsIzCIv+l9liuYLaMQ/" crossorigin="anonymous"></script>
  <script src="https://use.fontawesome.com/releases/v5.15.3/js/all.js" data-auto-replace-svg="nest"></script>

</head>

<body>

  <div class="input-group flex-nowrap">
    <span class="input-group-text" id="addon-wrapping">@</span>
    <div class="input-wrapper d-flex align-items-center position-relative">
      <input type="text" class="form-control ps-4" id="my-input" placeholder="E-mail" aria-label="Username" aria-describedby="addon-wrapping">
      <label for="my-input" class="fa fa-user input-icon position-absolute px-2" onclick="myFunction()"></label>
    </div>
  </div>

</body>

</html>

Upvotes: 1

Related Questions