Button Menu in HTML

When you click the menu button, I want the text inside the class="greetings" div to change from "Welcome Jon deo" to just "G"

So basically, whenever the menu button is clicked, display the letter G and hide or replace "Welcome Jon deo"

Additionally, if the page is being viewed on a tablet, automatically show the letter G instead of "Welcome Jon deo"

<html>
  <header>
    <title>This is title</title></header>
  <body>
  <button type="button">menu</button>
  <div class="greetings">
  <br>
    <span>Welcome Jon deo</p>
  </div>
    <div class="this">
      <br>
    </div>
    <span>Web Content</span>
  </body>
</html>

Answer №1

If you want to hide the text 'Welcome Jon deo', a simple solution is to change the <span> tags to <p>, and add an ID attribute so you can target it in your CSS. Additionally, insert another <p> element with its own ID for further manipulation using CSS and JavaScript. Make sure to assign an ID to your button as well for easy selection via JavaScript. I have indicated the specific modifications to be made in your HTML code below:

<html>
  <header>
    <title>This is title</title>

    ****UPDATE HERE***
    <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1.0">

  </header>
  <body>
  <button id="menuBtn" type="button">menu</button> ***MODIFY HERE***
  <div class="greetings">
  <br>
    <p id="text">Welcome Jon deo</p> ***MODIFY HERE***
    <p id="text2">G</p> ***MODIFY HERE***
  </div>
    <div class="this">
      <br>
    </div>
    <span>Web Content</span>
  </body>
</html>

Below is the CSS snippet to keep the 'G' hidden unless viewed on tablets or mobile phones:

#text2 {
  display: none;
}

To dynamically switch from displaying 'Welcome etc etc' to 'G', implement the following JavaScript function alongside your existing code:

const text = document.getElementById('text');
const menuBtn = document.getElementById('menuBtn');

// Click event listener for the menu button
menuBtn.addEventListener('click', changeText);

// Function to update text content
function changeText() {
    text.innerHTML = 'G';
};

In order to automatically showcase 'G' on smaller screens, adjust your meta tags in the HTML section (refer to the changes implied above) and apply media queries within your CSS code:

/* Viewport settings for extra small devices (phones, 600px and below) */
@media only screen and (max-width: 600px) {
        #text {
           display: none;
        }
        #text2 {
           display: block;
        }
}

/* Display settings for small devices (portrait tablets and large phones, 600px and beyond) */
@media only screen and (min-width: 600px) {
        #text {
           display: none;
        }
        #text2 {
           display: block;
        }
}

/* Handling of medium devices (landscape tablets, maximum width at 768px) */
@media only screen and (max-width: 768px) {
       #text {
           display: none;
        }
        #text2 {
           display: block;
        }
}

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

Implementing script loading within the Angular scope

I'm attempting to load a custom script from the database based on client-side logic. I am having trouble figuring out how to make it function properly. Here is my controller code: 'use strict'; angular.module('youshareApp') . ...

A plethora of options for popup modals in jQuery, including stacked modals and various alternative modal

Is there an alternative to using multiple modals? I require the ability to have multiple instances of modals. Upon clicking "Open modal" on the main page, a form modal (Modal-A) should open, with a link inside Modal-A that can open another modal (Modal-B) ...

What is the best way to format a date input field so that when a user enters a year (yyyy), a hyphen (-

Need help with date formatting in the format yyyy-mm-dd. Seeking a way to prompt user input for the year and automatically append "-" to the date as needed. Also utilizing a datepicker tool for selecting dates. ...

Make HTML design responsive to any screen size

After completing the design of a mobile app interface with dimensions 750w by 1334H, I encountered an issue where it appears too large on the app testing screen. I am seeking a way to automatically adjust the design to fit all screens without compromising ...

Do the Push Notification APIs in Chrome and Firefox OS follow the same set of guidelines and standards?

Do Chrome and Firefox OS both use Push Notifications APIs that adhere to the same standard? If not, is either one moving towards standardization? ...

How can I convert a PHP class into inline CSS styles?

While exploring MailChimp's css inliner at , I found myself curious if there are any available classes that can achieve the same result. It would be convenient to have this functionality directly in my email code without relying on MailChimp. I am es ...

Is it possible to capture a screenshot of a YouTube video at a specific moment?

Imagine a scenario where we have a function called getScreenShot. We invoke it in the following manner: scrShot=getScreenShot(videoID, time, quality) This function is designed to capture a screenshot of the video at the specified time (e.g. 1:23) and in t ...

Can simply adding Bootstrap CDN make a website responsive?

Is Bootstrap truly self-sufficient when it comes to responsive design, or do custom webpages utilizing Bootstrap require additional responsive instructions? If I incorporate the Bootstrap CDN and utilize its built-in components, can I assume the webpage ...

There seems to be a white space problem located beneath the header photo and the initial

I'm a beginner at coding and working on this project for fun. I'm almost done, but I can't get rid of a white space that's dividing my header from the first section. See the screenshot below: https://i.stack.imgur.com/a1flt.png Even af ...

Scroll to the end of the main div's horizontal position instead of using offset().left

I'm struggling with an issue in my code. <script type="text/javascript"> $(function() { $('ul.nav a').bind('click',function(event){ var $anchor = $(this); /* ...

Empower your presentations with slick cloning technology

I am facing an issue with my slider that I cannot seem to resolve. Currently, I am using slick to display 3 slides, but only the center one shows all the content. To achieve a continuous infinite slider effect where all slides appear in the center, I need ...

Submitting information to an HTML page for processing with a JavaScript function

I am currently working on an HTML page that includes a method operating at set intervals. window.setInterval(updateMake, 2000); function updateMake() { console.log(a); console.log(b); } The variables a and b are global variables on the HTML page. ...

What is the method for designing a relative layout that organizes elements horizontally?

Can you tell me why elements are typically arranged vertically in a column? Is it feasible to change their orientation and display them horizontally, like in a row instead of a column? ...

FullCalendar fails to load in Bootstrap 4 tab

My current project involves utilizing Bootstrap 4 tabs, and I encountered an issue with displaying a FullCalendar on the second tab. Specifically, I am using version 5.2.0 of the FullCalendar library. While the calendar functions correctly when placed on t ...

The component triggering the redirect prematurely, interrupting the completion of useEffect

I set up a useEffect to fetch data from an endpoint, and based on the response, I want to decide whether to display my component or redirect to another page. The problem I'm facing is that the code continues to run before my useEffect completes, lead ...

Is it possible to use styled-components to specifically style a child class within a component?

Hey there, I'm currently working with a mui Badge component and am trying to customize the appearance of the small circle. This is what I have so far: const BadgeStyled = styled(Badge)` & > .muibadge-badge: { background-color: #d3d4d7; ...

Can one utilize Javascript to write in plain text format?

Currently, using JavaScript I have a plain text containing data that is displayed within my form tags. Everything is functioning correctly, but now I need to update the values inside the code of my form tags in order for the changes to also be reflected in ...

Trigger the opening of a class or window upon pressing the button

Good evening, I'm attempting to create a functionality where pressing a specific button will open a window. However, I am unsure of how to achieve this using CSS classes. My initial thought was to add a new class in the CSS file and call it every ti ...

Trouble with implementing conditional styles in Next JS

<label className={`${darkModeActive ? 'text-brand-five' : 'text-brand-seven'} ${errors.invoiceDate ? 'text-[#EC5756]' : ''}`} htmlFor="invoiceDate"> Invoice Date </label> I am encountering a ...

Using Telegram markdown: Can you combine both bold *and* italic? (September 2018)

After referencing the Markdown Syntax guide on Telegram’s Wiki page, it appears to be quite straightforward to format text as bold and italic. The guide explains that: *this is in italic* and _so is this_ **this is in bold** and __so is this__ ***this ...