HTML button onClick event not triggering [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
Here is my HTML code:
const fps = 60
let score = 0
function GenerateAnswers() {
/*...*/
}
function UpdateTiles() {
/*...*/
}
function Correct() {
console.log("Correct")
score++
/*...*/
}
function Incorrect() {
console.log("Incorrect")
/*...*/
}
function MainLoop() {
// moving the tiles (does work)
// updating tile display (does work)
// updating score display (does work)
}
const mainLoop = setInterval(MainLoop, (1000 / fps))
div.AnswerBlack {
width: 100%;
height: 23.5%;
background-color: black;
color: red;
font-size: 10%
}
button {
width: 100%;
height: 100%;
transform: translateY(-11.2%)
}
<!--...-->
<table>
<!--...-->
<tr>
<!--...-->
<td id="Answer1">
<div class="AnswerBlack" id="tileID" style="transform: translateY(69%);">
ANSWER TEXT
<br>
<button onclick="Incorrect()"></button>
</div>
</td>
</tr>
</table>
The result of this is a moving tile (in which I control the movement of the tile by editing the translateY(x%)), and because there is a row a text in the div ("ANSWER TEXT"), I put "transform: translateY(-11.2%)" in the button style and after that the button does fill the div, but the problem is the Incorrect() function never got triggered despite having an Incorrect() function with a console.log function in the JavaScript file.
My goal is to make a moving black tile while the button in the div still works.
Note: I am not using jQuery and I do not want to use jQuery

Related

Show/Hide feature [closed]

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 4 years ago.
Improve this question
I'm trying to create a feature using either jQuery or vanilla JS. My feature should be able to show/hide content on click. The initial task is easy enough and I'm able to show my hidden div using jQuery toggle, but I would also like to be able to switch my FA icon as well as the link text from 'Show content' to 'Hide content'.
Use
.toggleClass(`.fa-check .fa-cross')
add one as the default.
Toggle text can be handled by either not using .toggle (using .show/.hide) instead or by including a span for each text and toggling those. Or by having 2 buttons and toggling those.
Example snippet (needs fa icons)
$(".toggle").click(function() {
$(this).toggleClass("collapsed expanded")
$(this).find("i").toggleClass("fa-chevron-up fa-chevron-down")
});
.wrapper { border: 1px solid #CCC; width: 200px; }
.wrapper .details { border-top: 1px solid #CCC; }
.wrapper .toggle.collapsed > div.details { display:none; }
.wrapper .toggle.expanded > .more { display:none; }
.wrapper .toggle.collapsed > .less { display:none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='wrapper'>
<div class='toggle collapsed'><i class='fa fa-chevron-up'></i>
<div class='more'>more</div><div class='less'>less</div>
<div class='details'>details</div>
</div>
</div>
You can apply the same logic for the text to the icon as well if preferred (include 2 <i class='fa...) and then use css to show/hide them based on the class on the parent.
You can also add some css transition so it's less jumpy, or you can use $(this).find(".more,.less").slideToggle() instead of just toggle to add an effect.
There are dozens of ways to do it. Below is a step-by-step tutorial.
This should work, assuming you have already included jQuery.
<img id="imgtog" onclick="toggle()" src="someimg1.png">
<div id="content" hidden="">content</div>
<script>
var tog = false;
function toggle()
{
if(tog == false)
{
$('#content').removeAttr('hidden');
$('#imgtog').attr('src', 'otherimage2.png');
tog = true;
}
else
{
$('#content').attr('hidden', '');
$('#imgtog').attr('src', 'someimg1.png');
tog = false;
}
}
</script>

How to disable all content, divs, etc so when server is processing [closed]

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 7 years ago.
Improve this question
I have an html page with some content including buttons, headers and etc. Every time a button is pressed the server does some processing which usually takes around 5-10 seconds. However during this time, the user can click on buttons, headers which will be unresponsive as the server is still processing.
How can i disable all the content(buttons, redirect headers, etc) on the page so that the user is not able to interact with the webpage when it is processing in the back, then re-enable everything after the processing is done?
Thanks for all the help
A quick and dirty way to do this would be to create a semi transparent div with a really high z-index positioned absolutely and the full size of the page. When a user clicks a button, show this div which will block interaction with all content under it. Then, when the server is done, hide the div. Something like this:
$('.myBtn').click(function() {
$('#result').append('Button Clicked!<br>');
$('#pageshield').show();
setTimeout(function() {
$('#pageshield').hide();
}, 2000);
});
.container {
position: relative;
}
#pageshield {
display: none;
z-index: 999999999;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: #000000;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
/* IE 8 */
filter: alpha(opacity=50);
/* IE 5-7 */
-moz-opacity: 0.5;
/* Netscape */
-khtml-opacity: 0.5;
/* Safari 1.x */
opacity: 0.5;
/* Good browsers */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div id="pageshield"></div>
<button class="myBtn">Click me</button>
<br>
<button class="myBtn">Click me</button>
<br>
<button class="myBtn">Click me</button>
<br>
<button class="myBtn">Click me</button>
<br>
<button class="myBtn">Click me</button>
<br>
<div id="result"></div>
</div>
Disclaimer, this works but does seem a bit hackish.
Also, if possible, consider changing the way your app works instead. In my opinion, blocking the whole page like this is bad UX. Which is the very reason the synchronous Ajax calls were deprecated
Try defining handlers for events as named functions , call .off() for each event attached to selector , reset handlers utilizing .on() when processing complete
$(function() {
var headerFn = function(e) {
alert(this.textContent)
}
var otherStuffFn = function(e) {
alert(this.textContent)
}
var arr = [{
type: "click",
fn: headerFn
}, {
type: "click",
fn: otherStuffFn
}];
var elems = $("header, div");
elems.eq(0).click(headerFn);
elems.eq(1).click(otherStuffFn);
$("button").click(function(e) {
e.preventDefault();
e.stopPropagation();
$("html").css("pointer-events", "none").fadeTo(250, .2);
$(this).next("span").text(function(_, txt) {
if (txt === "status: not active") return txt;
return txt.replace(/(active)/, "not " + "$1")
});
$.each(elems, function(key, el) {
$(el).off(arr[key].type)
});
setTimeout(function() {
$.each(elems, function(key, el) {
$(el).on(arr[key].type, arr[key].fn)
});
$("+ span", this).text(function(_, txt) {
return txt.replace(/not/, "")
});
$("html").css("pointer-events", "initial").fadeTo(250, 1);
}.bind(this), 15000)
})
})
header,
div {
width: 100%;
height: 150px;
padding: 8px;
}
header {
background: tomato;
}
div {
background: orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<button>click</button><span>status: active</span>
<header><i>click</i><br />header stuff</header>
<div><i>click</i><br />other stuff></div>

JS script not working properly [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
JS code:
function showCaption() {
var captionVis = $('.grey-box-caption-text').css('display');
if(captionVis = "none") {
$('.grey-box-caption-text').width(0);
$(this).find('.grey-box-caption-text').show().animate({'width': '464px'},750);}
} else {
$(this).find('.grey-box-caption-text').animate({'width': '0'},750,
function(){$(this).hide();});
}
};
$('.caption-container').click(function() {
showCaption();
return false;
}
);
HTML code:
<div class="one-half column home-three-img">
<div class="caption-container">
<div class="grey-box-caption">
</div>
<div class="grey-box-caption-text">This is a caption test - Hopefully this works
</div>
</div>
<img src="images/3.jpg">
</div>
This won't work and I'm a JS noob. Please help.
I'm trying to get the caption section to slide out from the left to the right. When i click on the parent container nothing happens. I'm expecting the caption to shoot out and hide when I click again.
I have Jquery loaded properly and have a document.ready function that works.
Link to the WIP http://clients.pivotdesign.com/dev/annual_report_2014/index.html
A big problem is that the value of this won't be set in your "showCaption" function. Add a return false; to the end of that function:
function showCaption(){
var captionVis = $('.grey-box-caption-text').css('display');
if (captionVis == "none") {
$('.grey-box-caption-text').width(0);
$(this).find('.grey-box-caption-text').show().animate({'width': '464px'},750);
}
else {
$(this).find('.grey-box-caption-text').animate({'width': '0'},750, function(){
$(this).hide();
});
}
};
and then change the handler assignment to:
$('.caption-container').click(showCaption);
Also note that your test in that if statement was incorrect: use == or === for comparison.
I modified the CodePen from Pointy a bit and it should do the same with less code.
Why the gray box increases the height here in the SO demo, I don't know. In this CodePen it works with-out this "bug".
function showCaption() {
var $graybox = $('.grey-box-caption-text');
$graybox.animate({
width: 'toggle',
opacity: 'toggle'
}, 'slow');
};
$(function(){
$("#wrapper").on("click", showCaption);
});
.grey-box-caption {
min-height: 3em;
min-width: 20px;
background-color: #bbb;
display: inline-block;
}
.grey-box-caption-text {
display: none;
padding: 1em;
font-family: sans-serif;
white-space: nowrap;
overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
<div class=grey-box-caption>
<div class=grey-box-caption-text>
Hello World this is some text.
</div>
</div>
</div>

Getting Div ID On Link Click [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What I am trying to do is fade out one box once the cross of that box is clicked but carn't seem to get it to work even by googling I got it to work when clicking the div it self by using $(this)but I want it to work when the cross is clicked.
<?php
require "global.php";
$select_staff = mysqli_query($database_connection,"SELECT * FROM staff_info");
if(!$select_staff) {
die("Database Error");
}
while($staff_info = mysqli_fetch_array($select_staff)) {
$div_id = $staff_info['div_id']; // assuming you have an id field in your DB
$account_name = $staff_info['account_name'];
$position = $staff_info['position'];
$description = $staff_info['description'];
echo "
<div class='staff_boxes' id='staff_boxes_$div_id'>
<a href='#' class='delete_button'> X </a>";
echo"
<h2>$account_name</h2>
$position
<p>$description</p>
</div> ";
}
?>
<script>
$( document ).ready(function() {
$('div').click(function() {
var myScript = document.getElementById('myScript');
$(this).removeAttr('id').fadeOut();
});
});
</script>
<style>
.staff_boxes {
width: 250px;
min-height: 150px;
border: 4px solid gold;
background:#C2DAD7;
/*to reduce float drop issues in IE*/
word-wrap: break-word;
}
.staff_boxes {
margin-left: 63px;
float: left;
}
/**Clear floats after the boxes**/
</style>
Thanks
Target the parent like so:
$('.delete_button').click(function() {
$(this).parent().fadeOut();
return false; //Without this the browser may scroll to the top of the page
});

how do i make a calling bar in javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have a simple sms interface written in javascript and every time i click on a call button, a calling bar will(something like a progress bar that never stops or status bar). i still have no code for it because im still a beginner..please help
i have here a link http://jsfiddle.net/XBppR/22/
function openPage()
{
if(some conditon)
opener.document.location = "http://www.google.com";
}
else{
// nothing, this else not required
}
}
this code is not working..don't mind this..for posting purposes only..
Warning: The following answer contains jQuery
So what you need to do is make two images, a foreground image to set the style:
(Can't see it very well, put it on a black background)
And a background image to create an animation effect:
Next, you must superimpose the FG on the BG.
HTML:
<div class="callbar">
<div class="callbarbg" style="width: 200px;"></div>
<div class="callbarfg"></div> <!-- later elements have higher z-index -->
</div>
CSS:
.callbarbg {
height: 20px;
background-repeat: repeat-x;
background-image: url("http://s9.postimage.org/4oij09p7j/sliding_Progress_Bar_BG.png");
background-position:right top;
}
.callbarfg {
position:relative;
top:-20px;
width: 200px;
height: 20px;
background-image: url("http://s9.postimage.org/bg8y34e73/sliding_Progress_Bar_FG.png");
}
.callbar {
overflow:hidden;
width:200px;
height:20px;
}
Finally, you must move the background image in order to make the fade in/out animation in each dot:
JS:
window.setInterval(function(){
var obj = $("parent of .callbar").find(".callbarbg");
if(!obj.data("width"))
obj.data("width", 200);
var w = obj.data("width") + 3;
obj.data("width", w).css("width", w);
var h = w%200;
if(h == 0 || h == 1 || h == 2){
obj.data("width", 200);
}
}, 33);
working jQuery jsFiddle with crude API: http://jsfiddle.net/pitaj/K25U8/6/
EDIT:
jQuery-free jsfiddle now up!
working jQuery-free jsFiddle with crude API: http://jsfiddle.net/pitaj/GpGE2/

Categories

Resources