This question already has answers here:
How to add/remove a class in JavaScript?
(13 answers)
Closed 1 year ago.
I would like to change a HTML class with JavaScript!
<div class="not-submitted-check" data-submit-check>
<i class="fas fa-check-circle"></i> Thank you for contacting us,
<button type="button" class="close"><i class="fas fa-times"></i></button>
</div>
I want the div class to change when i press the submit button
<button type="submit" class="submit" data-submit-button>submit</button>
const submitButton = document.querySelector("[data-submit-button]");
submitButton.addEventListener("submit", handleSubmit);
I want to create a function where the moment i press the button the classes change one with another
Use this:
element.classList.remove("oldclass");
element.classList.add("newclass");
Related
This question already has answers here:
How can I prevent refresh of page when button inside form is clicked?
(16 answers)
button not working when placed inside form
(3 answers)
Closed 8 days ago.
There is the following code:
<div class="my-page">
<!-- <form id="my-form"> -->
<h2>Clickable Dropdown</h2>
<p>Click on the button to open the dropdown menu.</p>
<fieldset id="my-fieldset">
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">1234</button>
<div id="File1" class="dropdown-content">
1
2
3
4
</div>
</div>
</fieldset>
<!-- </form> -->
</div>
If you comment out in it, then everything works correctly. Otherwise the work is wrong
Can someone explain what is wrong here?
A button has by default the type submit. Submit buttons in a form will submit the form, reloading the page.
If you want to use a button inside a form that does not submit the form, give it type "button" explicitly.
<button type="button" onclick="myFunction()" class="dropbtn">1234</button>
This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
What characters are valid for JavaScript variable names?
(12 answers)
Closed 3 months ago.
I am creating hover flip cards for an instructions manual, and I would like to create a button that would flip them all at once. To get the cards flipping, I only relied on CSS, but to make the button I need some javascript, which I have no idea how to use. Can anyone help me?
<script>
const flip-card = document.getElementByClassName("flip-card-inner")
flip-card.addEventListener("click",flipCard);
function flipCard() {
card.classList.toggle("flip-card-inner");
}
</script>
<body>
<button type="button" class="flipCard">Flip all</button>
<p/>
<div class="flip-card">
<div class="flip-card-inner">
<div class="flip-card-front">
<h5>Title</h5>
</div>
<div class="flip-card-back">
<ul style="font-size: 10pt;">
<li>Some text</li>
</ul>
</div>
</div>
</div>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Say I implement a small webbased game with JavaScript:
When game over, a menu pops up with multiple buttons. One button returns the player to the main menu.
The same button shall appear when I interrupt the game.
<div class="menu gameOverMenu" style="display: none;">
<button class="menuButton" type="button" id ="playAgain">
Play again
</button>
<button class="menuButton" type="button" id ="toMainMenu1">
Main Menu
</button>
</div>
<div class="menu pauseMenu" style="display: none;">
<button class="menuButton" type="button" id ="continue">
Continue
</button>
<button class="menuButton" type="button" id ="toMainMenu2">
Main Menu
</button>
</div>
and handle the button event to return to the main menu via jQuery:
$('#toMainMenu1, #toMainMenu2' ).on('click', function(){
switchUi("main");
})
I don't want to use the same id again and creating a similar button with an other ID as I did does not feel right. Should I create the button with the display: none; property once in my markup and clone it into my different menus? Create a class for these two buttons? Or is there an easier and better way to do so?
I hope I follow the SO rules of posting, this is my first time asking for help.
This is an option:
<div class="menu" style="display: none;">
<div class="gameOverMenu" style="display: none;">
<button class="menuButton" type="button" id ="playAgain">Play again</button>
</div>
<div class="pauseMenu" style="display: none;">
<button class="menuButton" type="button" id ="continue">Continue</button>
</div>
<button class="menuButton" type="button" id="toMainMenu">Main Menu</button>
</div>
or do not use ID toMainMenu1 and 2 but use a class nagivateToMainMenu and use that in your jQuery $("button.nagivateToMainMenu").on("click", function(){ switchUi("main"); }); etc.
<button class="menuButton nagivateToMainMenu" type="button" id="toMainMenu3000">Main Menu</button>
This question already has answers here:
Remove element by id
(19 answers)
Remove parent div by class name - jquery
(5 answers)
Closed 6 years ago.
Before you mark this as a duplicate, this is how my question is different.
Due to the lack of content actually on google, all that I can find is
removing the parent ID, I don't think that applies as the parent ID
would only remove panel-body, then leave panel-heading and the panel
div there.
I wanted to ask a quick question that I couldn't find any help with online. I have a bootstrap panel, which has two div's insdie of it, one for the panel-header and one for the panel-body. Inside the panel-body div I have a btn btn-success button that I want to remove the whole panel on click.
<div id="draggable" class="habbo-notification panel panel-default">
<div class="panel-heading">
Panel Header
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-4">
<font color="#B00049">
<h1 style="font-size:144px;margin-top:0;margin-left:14px;"><i class="fa fa-globe fa-6" aria-hidden="true"></i><h1>
</font>
</div>
<div class="col-md-8">
<p>
Some content here
</p>
<br>
<div class="btn btn-success" onclick="" style="width:100%">Close this</div>
</div>
</div>
</div>
</div>
</div>
In javascript or jquery (don't mind which one), how could I remove the entire div onclick of the button, without knowing the ID or class, or having any access to the panel div at all. I declare the panel-body's content somewhere other than the place where I declare the panel and it's two divs panel-body and panel-header.
I want to remove the whole panel div that the button is assigned to, on click?
<div class="btn btn-success" onclick="" style="width:100%">Close this</div>
Can anyone help?
The jQuery method is this one-liner.
$('.btn-success').on('click', function(){
$(this).closest('div.panel').remove();
});
The .closest() function looks for the nearest parent matching the supplied selector. In this case, it checks each parent of this until it sees div.panel.
You could drop this in your onclick="", though onevent attributes are deprecated in HTML. Standards expect events to be set within the Javascript.
I don't really get why you can't use the class(es) of the panel div like .panel, for example:
$(".btn").click(function(){
$(".panel").css("display","none");
});
But I think this would technically also work:
$(".btn").click(function(){
$(this).parent().parent().css("display","none");
});
This question already has answers here:
Greasemonkey, delete <a> element
(2 answers)
Closed 7 years ago.
I'm trying to remove the following line of code from http://trakt.tv/calendars/my/shows/ using Greasemonkey:
<a href="/vip">
<div class="huckster-vip-square">
<div class="inner">
<div class="text">
<h1>Support Trakt & become a VIP!</h1>
<h2>Hide advertising, unlock extended features and help Trakt grow.</h2>
<div class="btn btn-primary">Learn More</div>
</div>
</div>
</div>
</a>
How can I do that?
You may try it with jQuery.
Please refer to this question for using jQuery in Greasemonkey.
The JavaScript code would be like:
$("a[href='/vip']").remove();