Steps for adding a user-glyphicon within an img tag

In the given code, I am trying to create a function where if no photo is uploaded, a default bootstrap glyphicon-user image should be displayed. Once a photo is uploaded, it should overwrite the default image.

//Please ensure necessary Bootstrap files are included

<div class="row">
    <div class="row">
        <div class="col-md-4">
            <h4 class="heading">Upload Patient's Photo :-</h4>
        </div>
        <div class="col-md-4">
        <input type="file" class="form-control" name="patientpic" id="patientpic" onchange="readURL(this)"/>
        </div>
    </div>  

//This script is for previewing the uploaded image

    <script>
        function readURL(input) {
            if (input.files && input.files[0]) {
            var reader = new FileReader();  
            reader.onload = function (e) {
            $('#patientimg').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
            }
         }
        </script>                               
    </div>

//Changes have been made here to incorporate the Bootstrap glyphicon-user

    <div class="col-md-2 col-sm-12">
        <div class="thumbnail">
          <img src="" alt="Select Patient Image" class="img-responsive" id="patientimg" name="patientimg" style="height:140px;width:140px;font-size:30px;text-align:center;color:gray;"/>
        </div>
    </div>  

I attempted this setup, however, the glyphicon-user in the span tag appears below the patient image.

Answer №1

One idea to consider is converting Bootstrap's glyphicon-user to a PNG and then adding it to the code so that when a user uploads an image, it will be appended according to the existing code.

You can see a sample of this implementation here: http://jsfiddle.net/ws3bb7sr/1/

       function readURL(input) {
            if (input.files && input.files[0]) {
            var reader = new FileReader();  
            reader.onload = function (e) {
            $('#patientimg').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
            }
         }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<div class="row">
    <div class="row">
        <div class="col-md-4">
            <h4 class="heading">Upload Patient's Photo :-</h4>
        </div>
        <div class="col-md-4">
        <input type="file" class="form-control" name="patientpic" id="patientpic" onchange="readURL(this)"/>
        </div>
    </div>  
    
        <div class="col-md-2 col-sm-12">
        <div class="thumbnail">
          <img src="http://s5.postimg.org/4xpbh5oo3/user_000000_128.png" alt="" class="img-responsive" id="patientimg" name="patientimg" style="height:140px;width:140px;font-size:30px;text-align:center;color:gray;"/>
        </div>
    </div>  

Answer №2

It may not be the most ideal solution, but here's a code snippet you can try:

<html>
<head>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="//code.jquery.com/jquery-2.1.3.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
#img1 {
    width: 100px;
    height: 100px;
    margin-left: 25px;
}
#img1::before {
    position: absolute;
    font-size: 100px;
}
</style>
<script>
    $(function() {
        setTimeout(function() {
            $('#img1').attr('src', 'http://dummyimage.com/100x100/0ac72a/0011ff');
        }, 2500);
    });
</script>
</head>
<body>
<img src="" id="img1" class="glyphicon glyphicon-asterisk">
</body>
</html>

If you encounter issues with transparent PNG files, it's recommended to double-check them. You can also try removing the glyphicon classes when updating the image src if problems persist.

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

I would love to hear your suggestions for a custom select element plugin in jQuery

After browsing multiple options online, I decided to turn to the expertise of the Stack Overflow community to ask for recommendations based on personal experience. I am specifically in search of a plugin that can substitute a select element with a custo ...

What could be causing my randomly generated value to be overwritten so quickly?

I'm encountering an issue with my code. I am attempting to generate objects in random locations using drawHyperBall(), getRandomIntX(), and getRandomIntY(). However, the random value seems to be constantly overwritten. How can I resolve this problem? ...

The TD border is slicing through the div and is placed on the table in IE standards, causing it

Below is the HTML code where the div is being cut by the table border when the page is in standards mode. Interestingly, if I remove the <!DOCTYPE html>, then it works fine as expected. The issue of the outside div not behaving properly on the table ...

Arrange Drifting Components in a Fluid Layout Grid

I am in the process of creating a unique sticky note website where I want to arrange the notes in a grid format. As the window size decreases, the notes should automatically reorganize to fit the width and allow for vertical scrolling. My goal is to achiev ...

CSS styling on top points gets lost when scrolling

My screen displays an image with bubbles scattered randomly, each positioned using CSS styles for top and left. Strangely, when I scroll, all the bubbles lose their top style and reposition themselves to the bottom of the image. If anyone has insight into ...

Positioning a flash element in the center

Struggling to find the perfect alignment for a flash widget on this particular page . Experimented with using both the <center> tag and various other methods, but alas, it remains stubbornly off-center. If anyone has any suggestions or solutions, p ...

transform SQL data into HTML viewing

I am facing an issue trying to display information from a SQL database on an HTML page. Unfortunately, my code is not returning any results at all. <?php $host = "DRHATEM-PC"; $user = "sa"; $pass ...

The initial transition in offcanvas on bootstrap 5 is not appearing when a placement is dynamically added

I am currently working on triggering an Offcanvas with JS and making the placement configurable. The issue arises when attempting to dynamically set the offcanvas-end class to the offcanvas element, as it does not transition smoothly the first time it is t ...

Implementing Validation for DropdownChoices and Checkboxes in Wicket Java

There is a checkbox and dropdown menu on an HTML page. Upon clicking submit, if both are selected, there should be an error message stating that at least one of them is mandatory and prompting the user to select at least one value. To address this issue, ...

transitioning from font-awesome v4 to the latest version, v5

For a while now, I have been utilizing a responsive template. However, I am interested in updating its fa-module from v4 to v5. By downloading the free web package, replacing "font-awesome.min.css" with "all.min.css", and adjusting all references to the ...

The functionality of the Bootstrap grid system has proven to be ineffective

Hello, I am currently using Bootstrap 3 and attempting to configure my template. It seems like I may not have a full grasp on the concept of 'row' in Bootstrap because my template is not behaving as expected. Take a look at this full screen fidd ...

Step-by-step guide on inserting an image directly into an HTML file without utilizing LinkedResource or CDO

Let's say we have a scenario: My objective is to put together an HTML file with an embedded image, similar to the structure below: <html> <head> </head> <body> <img src="?" /> </body> </html> The quest ...

Struggling with my Transform Origin in CSS for SVG

I have two classes (firstCircle & spin) on the first circle on the left, and I'm attempting to make it rotate in place. After removing them from the css so you can see the circle, I am having trouble with transform-origin. My code seems wrong as i ...

Changing the position of elements based on screen resolution for Desktop and Mobile

I'm working on a page with a sidebar that contains elements on desktop. When viewed on mobile, the sidebar moves under the main page. However, I would like those elements to be displayed above the main page on mobile, similar to this: Any suggestions ...

Tips for adjusting the color of multi-line text when hovering with the mouse

I'm facing a challenge in changing the color of text when someone hovers over it, but so far I've only been able to make it work if the mouse scrolls over the top border of the text. The text I'm testing is located in the top left corner an ...

Improve the loading speed of large datasets into HTML tables

I have a problem where I generate an HTML table from code behind to display data like a grid (Note: I am not using GridView). When the data is large, around 2000 rows, it causes the application to display a white page as if it has hung. What is the best w ...

Is there a streamlined approach to reducing the code for an Angular Bootstrap card with a fixed length and height?

Is there a more efficient way to distribute a fixed number of cards with fixed dimensions in three rows? Currently, I am manually coding all 33 cards spread across 3 rows - 11 cards per row. Please excuse any mistakes in my code as I am still a beginner a ...

JavaScript has the ability to manipulate the width of an element by using methods to both retrieve

I have a unique situation where I need to dynamically adjust the width of a ul list element based on the text content inside it. Specifically, I want the width of the ul list to be equal to the width of the first list item, which can change frequently. My ...

When attempting to launch my JavaFX 2 application from an HTML page, it fails to execute

I recently downloaded the JavaFX 2 samples and encountered an issue when trying to run one of them from its HTML file. Specifically, I am referring to a JavaFX application called BrickBreaker, which consists of three files: BrickBreaker.jar, BrickBreaker.j ...

Creating a dynamic step animation with jQuery: A step-by-step guide

Struggling to find resources on how to animate a color wheel using jQuery? Want to create a spiraling effect by gradually revealing colors at 45-degree intervals, like a loading GIF spiral? I need it to cycle through the defined colors in order and then cl ...