Flickering HTML Form button radio Javascript true/false [closed] - javascript

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 6 years ago.
Improve this question
I would like to make a button which I can put on "true/false" on a certain time with javascript. I want to make a flickering button. How could I do this in Javascript?
I tried something like this:
<td colspan="1" class="button" tabindex="1">
<input type="radio" tabindex="-1">
</td>
__
setTimeout(function() {
$("*[tabindex='1']").find('input').get(0).checked = true;
http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_radio
(This button)

This checks/unchecks the button randomly and you can certainly change the logic behind when the button should be on vs. off, but it shows how to check and uncheck the radio button natively (no JQuery) with the addition or removal of the checked attribute on the Radio Button.
var btn = document.getElementById("rad");
setInterval(function(){
var rnd = Math.random();
console.log(rnd);
rnd > .5 ? btn.setAttribute("checked", "checked") : btn.removeAttribute("checked");
}, 500);
<input id="rad" type="radio">

Your code will work if you put the <td> element inside <table> tags.
If the <td> is outside of a table element, it is removed so you cannot access the radio button by searching for the parent td.
To make the button flicker, try having the function reset itself:
function buttonOnAfterDelay()
{
var $radioButton = $("*[tabindex='1']").find('input').get(0);
if ($radioButton.checked)
$radioButton.checked = false;
else $radioButton.checked = true;
setTimeout(function() {buttonOnAfterDelay()}, 1000);
}
buttonOnAfterDelay();

Related

jquery get value of specific number inputs in while loop [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 last month.
Improve this question
what the result looks like
I'm making a product inventory system; where I can simple change the value with number inputs, hit the checkmark, and be done. But I'm having trouble getting the specific value.
php:
<?php
$get_prod = $con->query("SELECT * FROM products");
while($get = mysqli_fetch_array($get_prod)){
echo '
<div class="prod-inv">
<div class="prod-inv-top">
<h1>'.$get['prod_name'].'</h1>
<h2>'.$get['prod_price'].'</h2>
</div>
<div class="prod-inv-body">
<img class="image-manage" src="'.$get['prod_cover'].'">
<input id="'.$get['prod_id'].'" type="number" class="num" value="1"/>
<button id="'.$get['prod_name'].'" class="btn search">✓</button></a>
</div>
</div>';
}
?>
jquery:
<script type="text/javascript">
$("button").click(function() {
var target = this.id;
var value = $(target).val();;
alert(value);
});
</script>
I've tried everything from form GET, to href links, now jquery. So far nothing has yelded results.
[1] Firstly, I'm not familiar with PHP, but it seems that closing anchor tag mistakenly presents in the markup:
<button id="'.$get['prod_name'].'" class="btn search">✓</button></a>
Or maybe opening anchor tag is missing...
[2] Secondly, in following line:
var target = this.id;
context "this" is currently clicked button, this code takes clicked button id and then in this line:
$(target)
you're taking currently clicked button DOM element by it's id, but it is already stored in "this"
[3] when you want to query element by id, you have to prepend it with hash "#":
$('#' + target)
So, you see the issue, when button is clicked, you take it's id, then find this button on the page again by it's id and then try to take .val() from a button (which actually does not have a value, it is not an input)
Suppose, you wanted to achieve following:
button is clicked -> find input near this button -> take value of this input
$('button').click(function () {
var value = $(this).closest('.prod-inv-body').find('input').val();
console.log(value);
});

jQuery Selector Test For All of a ClassType [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 6 years ago.
Improve this question
I have a table. The table data elements each have a div with a class in them. The div displays a small CSS box, which based on the background color gives the appearance of a light, which turns on or off.
I want to turn the lights on and off individually by clicking on them (at random), but then I need to test when any light is turned off, are all lights now off.
<table>
<tr>
<td>
<div class="lightbox lightbox-on">
</td>
</tr>
<tr>
<td>
<div class="lightbox lightbox-off">
</td>
</tr>
...
</table>
In other words, if the most recent click turned off the last "on" light, so that all lightbox-on classes have been removed and replaced with lightbox-off classes, trigger something.
The question goes to testing whether a group of divs all contain the same set of identical classes.
With JQuery you can do this quite easily with something like this:
$(".lightbox").click(function() {
// Remove the lastturnedon class from all lights
$(".lightbox").removeClass("lastturnedon");
if (!$(this).hasClass("lightbox-on")) {
// Add the the lastturnedon class to the clicked light only if we are turning it on.
$(this).addClass("lastturnedon");
}
// Changing the lights state (on/off).
$(this).toggleClass("lightbox-on");
if (!$(".lightbox").hasClass("lightbox-on")) {
// Alert if now that we turned off this light, all lights are off
alert("All lights are off");
}
});
Here is an example:
http://codepen.io/nathanfelix/pen/XdVrvo
I think the most direct thing to do is just turn off all of them before adding the on to the one you want to.
$('.lightbox').removeClass('lightbox-on');
After click or toggle, use .length to count the number of visible elements, something like:
if ( $('.lightbox-on:visible').length == 1) {
// only 1 lit, do something here
}
or
if ( $('.lightbox-on:visible').length == 0) {
// they're all off, do something here
}

I don't understand the concept of this function in Javascript [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 6 years ago.
Improve this question
for example, you would have
<input id="searchbox" type="text" value="Type Here..." />
<button id="clickbait">Click Me</button>
<div id="emptydiv">sda</div>
<script type="text/javascript">
var clickthis="clickbait";
var searchinput=document.getElementById("searchbox").value;
var generate="emptydiv";
document.getElementById(clickthis).onclick=function () {
searchinput=document.getElementById("searchbox").value;
document.getElementById(generate).innerHTML=searchinput;
}
</script>
So this is pretty much just something to change the text in the empty div to whatever is in the text field when you click on the button but I don't understand the "idea" of what's going on here. I already set the var to a value so why do you need to grab the value again?
I can understand the other basics so far like document(refers to the whole page) then getelementbyid(refers to the element) and then on the click you want it to perform a function but everything after that is confusing.
I already set the var to a value so why do you need to grab the value
again?
When you set the searchinput value first before the onclick assignment, you fetched and assigned the value of searchinput at that time.
Inside the click event handler, you are getting the value in searchinput when the div is clicked so the value at that time might be different.
When its firing click event then searchinput=document.getElementById("searchbox").value;
that means searachinput is what everever typed in the input feild
and then that value is being reassigned to
document.getElementById(generate).innerHTML=searchinput;
`
When the button is clicked fire the event:
document.getElementById(clickthis).onclick=function ()
Get the value of 'searchBox':
searchinput=document.getElementById("searchbox").value;
Assign what ever was in the 'searchBox' as HTML in 'emptyDiv'
document.getElementById(generate).innerHTML=searchinput;
Just to note that:
searchinput=document.getElementById("searchbox").value;
Is actually redundant because you are already declaring it with the other vars.
So your code should look like this:
document.getElementById('clickbait').onclick=function () {
var searchinput = document.getElementById("searchbox").value;
document.getElementById('emptydiv).innerHTML=searchinput;
}
Or to make it even simpler and cut it down to 2 lines you could even use jQuery:
$('#clickbait').on('click', function(){
$('#emptydiv').innerHTML = $('#searchbox').value;
}
It copies the value of input into the "emptydiv" div when you click on the button.
Why is the real good question, here, IMO.

How to combine 2 button to 1 button in jquery [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 7 years ago.
Improve this question
$(document).ready(function(){
$("#sakla").click(function(){
$(".askforum").hide();
});
$("#getir").click(function(){
$(".askforum").show();
});
Hi ! how can I combine two button function to one button ? I want to run two functions with one button. the button name should change when I clicked if its show it must be become hide. thanks
Use toggle to toggle between shown and hidden. Change the two buttons into one button, and bind the click event to that button alone.
$(document).ready(function(){
var button = $('#thatnewbutton');
button.click(function(){
var forum = $(".askforum");
forum.toggle();
button.text(forum.is(':visible')?'Hide':'Show');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="thatnewbutton">Hide</button>
<div class='askforum'>Content</div>
Here is a simple demo:
$(function() {
$('.toggle').on('click', function() {
var txt = $(this).text().trim();
$(this).text( txt == 'HIDE' ? 'SHOW' : 'HIDE' );
$('.whattotoggle').toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button class="toggle">HIDE</button>
<div class="whattotoggle">Toggle This</div>
$(document).ready(function(){
$("#thatnewbutton").click(function(){
$(".askforum").toggle();
});
});
Alternatively for changing the name, find attr name of the button and accordingly change it using attr function.
How can I set the Name attribute of an input field using Jquery?
Cheers!

How to add new text field when button is clicked [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 want to create a form for my client. My client require to create a dynamic for him.
Suppose, I want to insert 1 record in main table of mysql, and record multiple records in secondary table which has reference key of main table. I don't know how many records against the main table, it maybe one at time or multiple records at time. I want to do it with single form. If client click add more button it show another text field to insert more data.
how I can do it ?????
It would be possible using pure javascript
like this
<input type="button" onclick="addInput()"/>
<span id="responce"></span>
<script>
var countBox =1;
var boxName = 0;
function addInput()
{
var boxName="textBox"+countBox;
document.getElementById('responce').innerHTML+='<br/><input type="text" id="'+boxName+'" value="'+boxName+'" " /><br/>';
countBox += 1;
}
</script>
This is possible through the jquery/javascript.
So you can use this reference :http://www.mkyong.com/jquery/how-to-add-remove-textbox-dynamically-with-jquery/
Just try it!!
here is the code:
function generateTextBox()
{
var mainDiv=document.getElementById('options');
alert(mainDiv);
var newBreak=document.createElement('br');
mainDiv.appendChild(newBreak);
for(var i=1;i<=4;i++)
{
var newTextBox=document.createElement('input');
var newBreak1=document.createElement('br');
var newBreak2=document.createElement('br');
var newBreak3=document.createElement('br');
var text = document.createTextNode("Option"+i);
newTextBox.type='text';
newTextBox.setAttribute('id','txtAddr'+i);
alert(newTextBox+"2");
mainDiv.appendChild(text);
mainDiv.appendChild(newTextBox);
mainDiv.appendChild(newBreak1);
mainDiv.appendChild(newBreak2);
mainDiv.appendChild(newBreak3);
}
}
This isn't php related, you'll need to dynamically add to the form using javascript.

Categories

Resources