Switch Button in HTML

I am trying to implement a toggle button on my website, but unfortunately it is not working properly.

Here is the HTML code I am using:

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" media="screen">
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=true"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
  function initialize() {
    var latlng = new google.maps.LatLng(47.583937, 13.963623);
    var myOptions = {
      zoom: 7,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

  }

</script>
<script type="text/javascript">
$(function(){
    $('.slider-button').toggle(function(){
        $(this).addClass('on').html('Quiz');
    },function(){
        $(this).removeClass('on').html('Read');
    });
});

</script>

</head>

<body onload="initialize()">
<header id="site_head">
        <div class="header_cont">
        <h1><a href="#">mr. hurley</a></h1>
        </div>
</header>

<div id="menu" style="float: left; width:20%; height:100%;">
    <ul>
        <li class="first"><a href="#">Search</a>

        <div class="slider-frame">
            <span class="slider-button">OFF</span>
        </div>
        </br>
        <p> [Lorem ipsum text]</p></li>
        <li class="active"><a href="#">Offer</a><p> Placeholder</p></li>
    </ul>
</div>

 <div id="map_canvas" style="float: left; width:80%; height:100%"></div>

   <div id="over_map">
    <p>TEst dsfkjsldökjfdslkäfjsdfsdfsdfsdfs</p>
   </div>
</body>


</html>

And here is the CSS styling associated with it:

[CSS Styling Code]

I have referred to this example for guidance: http://jsfiddle.net/vY7SQ/3/

If anyone could provide some assistance, it would be greatly appreciated. Thank you!

Answer №1

In the event that the jQuery script fails to execute, consider eliminating the onload="initialize()" attribute from the HTML and instead invoke initialize() within the $() function.

Answer №2

For a different approach, consider altering

<span class="slider-button">
to <span id="slider-button"> and then utilize
$('#slider-button').toggle(function(){
instead of
$('.slider-button').toggle(function(){
The Jquery addClass method simply adds a class rather than replacing it.

Remember to update your CSS as well.

Answer №3

In my opinion, it would be better to refactor the jQuery code as follows:

$(function(){
    $('.slider-button').toggle(function(){
        $(this).addClass('on');
        $(this).html('Quizz');
    },function(){
        $(this).removeClass('on');
        $(this).html('Read');
    }); });

Check out the updated JSFiddle link

Update: Despite receiving some conflicting feedback, scubafly seems to support my initial suggestion.

I still stand by my recommendation: Attach the toggle function to .slider-frame and make changes on .slider-button This approach is more user-friendly.

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

Display the page number when printing using CSS

I am struggling to display the page number at the bottom of my printable document. I followed a source on Stack Overflow, but unfortunately, it did not work for me. My current CSS attempt to achieve this is as follows: body { text-align: justify; } /* ...

Tips for creating PrimeNG tables with columns that automatically adjust in size

Is there a way to automatically adjust and resize the columns in my PrimeNG table? I'm looking for a method to make this happen. Can you help me achieve this? ...

How can I turn off autocomplete in MUI textfields?

Currently, I am working with the latest version of mui. Within my user contact info form, there is a zip code field that I do not want to be auto completed if the value is null. However, despite my efforts, it continues to autocomplete with the email saved ...

Implementing JavaScript to override inline CSS styles

Is it possible to override inline CSS using JavaScript with compatibility for IE6? I came across a pure CSS solution, but unfortunately it doesn't work in IE. http://www.sitepoint.com/blogs/2009/05/27/override-inline-css/ <div class="block"> ...

The AJAX request to the GitHub OAuth API using jQuery encountered an unknown issue and returned an unexpected error

Let me start by sharing the following code snippet: var requestAuthUri = 'https://github.com/login/oauth/access_token'; var ajaxArgs = { type : 'POST', url : requestAuthUri, headers : { &ap ...

Using multiple parameters in a GET request

url: "../api/api.php? fxn:" + encodeURIComponent(getCatergories) & "jsn"= +encodeURIComponent{"code":"1"}, var app = angular.module('MyTutorialApp',[]); app.controller("MainController", function($scope,$http){ $scope.loadpeople= functio ...

What are some ways to prevent "window.onbeforeunload" from appearing when a form is submitted?

Is there a way to prevent the alert box from appearing when submitting a form using the Submit button below? I want to avoid being prompted to "Leave this page" or "stay on this" after submitting the form. <script> window.onbeforeunload = ...

Create a Material UI Text Field that has a type of datetime and can span multiple lines

Is it possible to create a Material UI component of type "datetime-local" that can be displayed on multiple lines while still allowing the date to be edited? The issue arises when the width is too narrow and cuts off the date text. Although the TextField ...

"Is there a way to implement a sequence of fadeIn() followed by fadeOut() and then insertAfter

I have created a simple div fade cycle. Some of the divs are within an outer div, which is then placed in another outer div. I've written a script that cycles through these divs by fading them in and out. However, there seems to be a problem - the div ...

Display issue with alert box

I run a website where users can submit posts and receive alert messages (success or warning) when they do so. The alert messages are displayed at the top of the page using position: absolute to prevent them from affecting the layout when they appear. Every ...

Is it possible to implement dependency injection within a .css document?

I have a C# .NET 6 application. Some of the web pages (Razor Pages) in the app use dependency injection to inject configuration into the Razor Pages (.cshtml files), allowing certain config elements to be displayed in the user interface. My query is, can ...

Using Javascript and Node.js to send a JSON request

While browsing through this particular question, I came across a method in node.js to distinguish between html requests and json requests: app.get('/route', function (req, res) { if (req.is('json')) res.json(data); else if (req ...

What is the best way to exclude HTML tags from the innerHTML attribute?

Currently, I am in the process of developing a messenger application and facing an issue where my messages are not ignoring HTML tags. This is happening because I directly paste the input text into the innerHTML of the message. The code snippet responsible ...

When a sidebar is added, Bootstrap 5 cards that are supposed to be displayed in a row end up being shown in a column instead

When I have a row of Bootstrap 5 cards that display correctly, but then add a sidebar that causes them to stack in a column instead of remaining in a row. How can I maintain the row layout even with the addition of a sidebar using specific CSS classes or p ...

Update the appearance based on the dimensions of the parent container with Bootstrap

Here's the situation I'm facing on my webpage: <div id="myDiv"> <p>A</p> <p>B</p> </div> If 'myDiv' is narrower than width X, display A & B vertically. Otherwise, display A and B horizo ...

What is the reason behind every element in bootstrap being enclosed within an anchor tag?

I am currently delving into the world of bootstrap for the first time and encountering an issue with the grid system. Somehow, whenever I try to incorporate an - a href="#" - tag within the grid, it distorts the layout inexplicably. I have been struggling ...

Dynamically resizing a property in the DOM with Angular

Could someone help me with an issue I'm having regarding resizing items on the screen in Angular when the browser window size changes? Here is my code snippet: // TS File height: number = 2.5; objHeight:number; screenHeight:number = window.innerHeig ...

CORS request results in unsuccessful cookie setting in browsers except for Firefox where Set-Cookie header is detected; notably, Chrome does not show the header

I'm experiencing an issue with setting cookies from a NodeJS server to the client using JQuery and AJAX. Despite seeing the Set-Cookie headers in the response, the cookies are not showing up in the document.cookie string. I would greatly appreciate it ...

Bootstrap - labels with several words may split into separate lines

When I create a form with input fields and labels in an ASP.NET view, I notice that labels with multiple words break into separate lines at full scale. I would like to keep these labels on one line for better aesthetics. Here's an example: @using (Ht ...

Creating a grid layout with images encapsulated within a div using HTML

I am struggling to create a grid of images with padding between them inside the main_block div. The issue I'm facing is that I can't get the images to align next to each other using inline block or break them with a because they go in line ins ...