Is there a way to display the icon in Javascript?

I added a glyphicon element to my page but set its visibility to hidden using CSS. My goal is to reveal the glyphicon (#gl1) when users input text and click the "post" button, but I'm encountering an issue where the glyphicon doesn't show up when triggered by JavaScript. I'm not sure why this is happening.

Here is my code: My CSS :

    <html>
    <head>
    ...
    </head>

In my HTML elements, upon clicking the "post" button with id="post", the glyphicon should become visible...

    <body>
    ...

JS Code :

    <script>
    function ClickPost(){
    ...
     }   

    </script>
    ...
    </body>    
    </html>

Upon checking the console, I see the following error message: Uncaught TypeError: Cannot read property 'style' of undefined at ClickPost (Post edit and delete.html:94) at HTMLButtonElement.onclick (Post edit and delete.html:67). Can someone please assist me with this issue?

Answer №1

If you use this command window.post.innerHTML = txt;, it will update the content of the div#post element with a new value of txt. This means that all other elements within the same div#post will be replaced, including the 3 spans.

To preserve the spans, you can insert another inner element inside the div#post, like so:

<div id="post" onmousedown="">    
    <span id="gl1" class="glyphicon glyphicon-cog" ></span><br>    
    <span class="glyphicon glyphicon-pencil" id="gl2"></span>
    <span class="glyphicon glyphicon-trash" id="gl3"></span>   
    <span id="postContents"></span> <!-- this is where -->
</div>

function ClickPost() {
  var txt = window.txtar.value;
  window.postContents.innerHTML = txt;
  window.txtar.value = "";
  if (!txt) {
    window.postContents.innerHTML = "Please write something ..!";
    window.post.style.color = "#a80707";
  } else {
    window.gl1.style.visibility = "visible";
    window.post.style.color = "";
  }
}
div {
  background-color: beige;
  height: 400px;
  width: 500px;
  padding: 10px;
  border: 3px solid brown;
  margin: 20px;
}

div #txtar {
  width: 470px;
  height: 280px;
  font-size: 30px;
  padding-left: 13px;
  resize: none;
}

div .btn {
  float: right;
  margin-top: 40px;
  margin-right: 9px;
  width: 80px;
  height: 40px;
  font-size: 17px;
  font-family: monospace;
  font-weight: bold;
  background-color: beige;
  border: 1px solid brown;
}

#post {
  width: 470px;
  height: 500px;
  background-color: white;
  border: 1px solid brown;
}

#post .glyphicon-pencil,
.glyphicon-trash {
  float: right;
  color: brown;
  font-size: 20px;
  margin-left: 10px;
  visibility: hidden;
}

#gl1 {
  float: right;
  color: brown;
  font-size: 20px;
  margin-left: 10px;
  visibility: hidden;
}

#gl1, #gl3 {
  visibility: hidden;
}

#post span {
  border: 1px solid brown;
  padding: 10px;
}

#post #postContents {
  border: none;
  padding: 0;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

<div>
  <textarea id="txtar" placeholder="Write Something ..."></textarea>
  <button class="btn" onclick="ClickPost();">Post</button>
  <button class="btn" onclick="">Cancel</button>
</div>
<div id="post" onmousedown="">
  <span id="gl1" class="glyphicon glyphicon-cog"></span><br>
  <span class="glyphicon glyphicon-pencil" id="gl2"></span>
  <span class="glyphicon glyphicon-trash" id="gl3"></span>
  <span id="postContents"></span>
</div>

Keep in mind that I made some minor adjustments to your CSS, HTML, and JS while retaining most of your original code.

P.S. Feel free to ask if you have any questions.

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

Using Object Value as Variable instead of String in JavaScript/React

When working with React & JavaScript, I am storing an input name as a string in an object: window.ObjectOne = { ObjectOneKey: "MyInputName", } The input name can be an object path like "Object.FirstName" or a variable name "MyVariableName" What I am at ...

What could be the reason for the selection box in my form not changing the items when togg

I'm having an issue with my form selection box code that used to work but is now not functioning properly. Could someone please help me identify where the error lies? Essentially, I have a form with the ID #main and a select box named #chart-type. Th ...

What is the rationale behind calling the open() and send() functions after the anonymous function() in the following code snippet?

function fetchData() { var request = new XMLHttpRequest(); request.onreadystatechange = function() { if (request.readyState == 4 && request.status == 200) { document.getElementById("content").innerHTML = request.responseText; } } ...

Automatically dismiss a Modal Popup without the need for a button or click event

I am currently facing an issue where I have a page constantly refreshing, and I am looking to implement a modal popup with a message saying "Please wait..." while the page reloads using $state.reload(). GenericModalService.confirmationSplit($rootScope, m ...

Angular data binding between an input element and a span element

What is the best way to connect input texts with the innerHTML of a span in Angular6? Typescript file ... finance_fullname: string; ... Template file <input type="text" id="finance_fullname" [(ngModel)]="finance_fullname"> <span class="fullnam ...

"Utilize CSS Flexbox to create a layout with set width and uniform

Within a container, I have organized three sections. As I adjust the size of my browser to a maximum width of 668px, I want section 1 and section 3 to be displayed in one row while section 2 is placed below them. The width of section 2 should be proportion ...

Using the index property within *ngFor in Angular versions 2, 4, and 5

When using Angular 2, 4, or 5, you cannot directly use $index. To utilize the index, you need to define it first. <li *ngFor="let item of items; let i = index">{{item}} - {{i}}</li> {{i}} represents the index of items However, in Angular ...

Tips for avoiding deleting content within a span element when using contenteditable

I am working on an HTML 5 editor that utilizes the contenteditable tag. Inside this tag, I have a span. The issue arises when all text is removed as the span also gets removed. I want to prevent the removal of the span, how can I achieve this? Here is a s ...

Latest News: The store is now received in the mutation, instead of the state

An update has been made to this post, please refer to the first answer After thorough research, I couldn't find a solution to my issue despite checking several threads. This is my first time using the Quasar framework and it seems like I might have o ...

Adjusting the height of an iframe in real-time

Looking for some help with adjusting the height of an iframe based on its src content. I have a basic understanding of JavaScript, but need some guidance on how to achieve this. Here is what I currently have: <iframe id="frame" scrolling="no" framebord ...

Styling Arrow Containers with CSS

Is there a way to style this box using CSS? I've come across tutorials on creating boxes with arrows but none of them seem to work for my specific situation. ...

Providing access to information even in the absence of JavaScript functionality

Is it possible to make the content of a webpage using jQuery/JavaScript visible even when JavaScript is disabled? Currently, I have a setup where clicking on an h2 header will display information from div tags using the jQuery function. I've made sur ...

`The error "mockResolvedValue is not recognized as a function when using partial mocks in Jest with Typescript

Currently, I am attempting to partially mock a module and customize the return value for the mocked method in specific tests. An error is being thrown by Jest: The error message states: "mockedEDSM.getSystemValue.mockResolvedValue is not a function TypeEr ...

Having difficulty with sending an AJAX GET request to connect to mongodb

I've been facing a challenging task of displaying data from a mongodb collection on the browser using nodejs and express. Here's the client-side call: document.onload= $(function (e) { var i = 0; $.ajax({ type: "GET", url: "http://localh ...

Tips for exchanging divs in a mobile view using CSS

Illustrated below are three separate images depicting the status of my divs in desktop view, mobile view, and what I am aiming for in mobile view. 1. Current Status of Divs in Desktop View: HTML <div id="wrapper"> <div id="left-nav">rece ...

Struggling with Getting My Animation to Function Correctly

I am trying to create a JQuery Animation that will move a div covering a text box up into the border when clicked. Despite multiple attempts, I can't seem to get the animation to work properly. Any suggestions? JavaScript function moveup(title,text ...

There seems to be a glitch with certain div elements not behaving as expected in Bootstrap version

I've been struggling with a persistent question lately that seems like it should have a simple solution, but I just can't seem to figure it out. The issue I'm facing is related to Chrome and how certain divs on this particular page behave ...

Maintaining Focus on Textbox within a Nested Update Panel in Asp.NET

Currently, I am in the process of developing relatively straightforward post and reply/forum pages using asp.net with C#. While everything seems to be functioning properly, the addition of update panels has left me feeling incredibly frustrated. To displa ...

Update the label for the weekday on Material UI picker

Is there a way to change the weekday display in Material UI DatePicker from single initial (M, T, W, T, F, S, S) to three-letter initials (MON, TUE, WED, etc)? I want to customize it in my component like this: <DatePicker disablePast disableTool ...

The error message "the property '_env_' is not found on the type 'Window & typeof globalThis - React / Typescript ERROR" indicates that the specified property is not present in the

I encountered an issue while working with React/TypeScript. When I perform this action within the component: <p>API_URL: {window._env_.API_URL}</p> The error message I receive is: property '_env_' does not exist on type 'Window ...