Alteration of icon size in input field using jQuery unintentionally

My issue involves an input field where users can input text from an array of emojis. When a user selects an emoji, it should display as an icon styled by an external stylesheet. However, there are two problems:

  1. The name of the icon appears on top of the displayed icon.
  2. The size of the input field changes when an icon is shown.

I want to resolve both of these issues.

Below is my current code snippet:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.inArray demo</title>
  <style>
  div {
    color: blue;
  }
  span {
    color: red;
  }

  .em{
  font-color:white;
  }
  </style>
  <script src="jquery-1.10.2.js"></script>
  <link href="emoji.css" rel="stylesheet">

</head>
<body>
 <div class ="see"></div>
<code>HEY</code>
 <input class="input">
 <div class ="see"></div>
  <div class ="see2"></div>
    <div class ="see3"></div>



<div class="add"></div>



<script>
var emojiss = [ "--abc", "--woman","--eye"];
var emoji=["-angel","-alien","----1","---1","--100","--1234","--8ball","--a","--ab","--abc","--abcd","--accept","--admission_tickets","--adult","--aerial_tramway","--airplane","--airplane_arriving","--airplane_departure"]

var input=$(".input");
var input2=$(".input").val();



 $(input).change(function(){

$(".see").html( $(".input").val());


if(jQuery.inArray($(".input").val(), emoji) != -1) {

var see=$(".see").html().replace('--', '').replace('--', '');


$(".see2").html(see);

var classs =see+"";

alert($(".see").html());




<!-- $(".see3").html(real); -->


    $(this).addClass("em em"+$(".see").html()).removeClass("see");

} else {
    alert("is NOT in array");


} 

});






</script>

</body>
</html>

Answer №1

The reason for this issue is due to the addition of the em class in your code:

$(this).addClass("em em" + $(".see").html()).removeClass("see");
__________________^^

If you check the emoji.css file, you can find the CSS rules for the class:

.em {
    height: 1.5em;
    width: 1.5em;
    background-position: center;
    background-repeat: no-repeat;
    background-size: contain;
    display: inline-block;
    vertical-align: middle;
}

You may also refer to the following JavaScript and CSS snippets:

var emojiss = ["--abc", "--woman", "--eye"];
var emoji = ["-angel", "-alien", "----1", "---1", "--100", "--1234", "--8ball", "--a", "--ab", "--abc", "--abcd", "--accept", "--admission_tickets", "--adult", "--aerial_tramway", "--airplane", "--airplane_arriving", "--airplane_departure"];
var input = $(".input");
var input2 = $(".input").val();


$(input).on('blur', function() {
  var selected_value = $(this).val();

  if ($.inArray(selected_value, emoji) !== -1) {
    $(".see").html('<i class="em em' + selected_value + '"></i>');
  } else {
    alert("is NOT in array");
  }
});
div {
  color: blue;
}

span {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://afeld.github.io/emoji-css/emoji.css" rel="stylesheet" />

<code>HEY type "-angel" for example in the input then blur the focus</code>
<br><br>
<input class="input">
<br><br>
<div class="see"></div>
<div class="add"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

difficulties arise when adjusting font size in html and css

When I first created an all HTML/CSS website, I used clickable images as a menu that scaled perfectly on all monitors and browsers. Now, for my new project, I wanted to use a background image for the menu with hyperlinked text on top. I thought setting the ...

A dynamic JQuery carousel displaying a variety of albums

I'm searching for a jQuery gallery or slider that can accommodate multiple albums without needing to load a new page each time. I don't need anything too fancy, just the ability to have links above the content. I have knowledge in html, css, and ...

Use Bootstrap to effortlessly arrange columns horizontally in a row

I'm facing a scenario where I need to arrange the columns in a row horizontally. The challenge is to ensure that the row expands horizontally to fit the columns. I attempted to use white-space: nowrap on the row, remove floats on columns, and set the ...

PHP failing to deliver response data in AJAX request

I'm really stumped on this one. I can't seem to figure out why this code isn't working, even though everything seems right. Here's what I have: On my basic HTML page, I have a JQuery script that makes an AJAX call to a PHP script in th ...

Is it possible to validate the fields on an HTML page that does not include a form tag using jQuery?

Below is a code snippet from my HTML page. Is it possible to validate the two specified text fields using jQuery without using any form tag in my HTML page? <div id="panel1"> <fieldset id="fieldset1"> <legend>Registration Details:</ ...

When placing the script URL with a pound sign into the DOM, it gets truncated

When I receive URLs for trackers from an external resource, they often contain a # symbol which causes the URL to be cut off after the pound sign when trying to execute/load it in the DOM. Unfortunately, these URLs are provided by a 3rd party and I have no ...

The container is not adjusting to the screen size correctly and the flex-direction: row is not functioning as

Struggling with setting up a basic layout in Angular for my application. While I have experience with Angular, the actual HTML/CSS design is new to me. No matter what I try, I can't seem to get this container to take up the whole screen width. Variou ...

Button with tooltip for easy access to trigger a modal dialog

My task involves creating a button with a tooltip that displays simple statistical information to the user, such as a student's grades (e.g., "Homework: 100%, Exams: 85%"). Upon clicking the button, a modal should appear with more detailed information ...

Utilize Angular to initiate the transmission of attribute values upon a click event

My question may be simple, but I've been struggling to find an answer. How can I send attributes or item property bindings of an item through a click event in the best way? For example: <item class="item" [attr.data-itemid]="item.id ...

Tips for showcasing information on an HTML website

I am currently facing an issue with displaying addresses retrieved from a database in my project. The problem lies in the formatting of the address when it is displayed on the page. Currently, the address is being displayed as follows: Address: 1 Kings S ...

Laravel jQuery Issue: Uncaught TypeError - Unable to Access 'value' Property of Undefined

Uncaught TypeError: Cannot read properties of undefined (reading 'value') at $.<computed>.<computed>.menufocus (jquery-ui.js:5911:50) at HTMLUListElement.handlerProxy (jquery-ui.js:626:7) at HTMLUListElement.dispatch (jque ...

Utilizing Cookies within an HTML Page

My current code is functioning perfectly, accurately calculating the yearly income based on the input "textmoney." I have a link to a more advanced calculator for a precise prediction. My goal is to find a way for the website to retain the data input from ...

Ways to reduce the size of images within a bootstrap carousel

I'm currently working on creating a carousel that will serve as a background image cover for my website, similar to the revolutionary slider in WordPress but without using a plugin. I am developing it specifically for my static website. The challenge ...

Ways to display additional selected values in a select menu

I have a basic form on my website that includes options like: Select 1 and Select 2. I would like to set it up so that if I select value Home in Select 1, the value in Select 2 changes to something like residence. How can I achieve this using a database? ...

Encountering a Blank Area at the Top of a Printed AngularJS Screen

Currently, I am tackling an issue in AngularJS while working on the Print Invoice Page. The problem I am encountering is a blank space at the top of the printed screen. Check out the image here Below is my code: HTML <div id="invoice" class="compact ...

The crossIcon on the MUI alert form won't let me close it

I am facing an issue with my snackBar and alert components from MUI. I am trying to close the alert using a function or by clicking on the crossIcon, but it's not working as expected. I have used code examples from MUI, but still can't figure out ...

Expanding Beyond Boundaries: Utilizing CSS to Overflow From a Div and Fill the Entire Screen

I have a main DIV that acts as part of my responsive layout grid. It grows to the maximum width I've set, which is 1280px, and then adds margins for larger devices. Below you'll find my CSS along with a snippet of Less code. .container { mar ...

Difficulties encountered when trying to interact with buttons using JS and Bootstrap

I'm working with a Bootstrap 5 button in my project and I want to access it using JavaScript to disable it through the attribute. Here is the code I've been testing: Script in Header: ` <script> console.log(document.getElementsByName(& ...

Tips for stopping the loading of background images on an image slider

I am utilizing unslider to showcase an image slider on the landing page of my website. The slides are designed with background images in the CSS, and they adjust based on the media query. My concern is that I don't want all the slider images to load a ...

The confusing case of jQuery's e.preventDefault: Unable to submit form despite preventing default behavior

Objective: Prevent the 'submit' button from functioning, validate fields, generate popover alerts for results, and submit form upon closing of popover. To achieve this, I have set up a hidden popover div. When the submit button is clicked, I uti ...