|
Hello Richard,
Thank you very much sir for your awesome response.
Just one follow up question.
So your solution and my JS would work together as shown below?
$(function(){
const displayShortCosts = function(){
$(".consultsup").show();
$("#btconsultsup").hide();
});
if ($("#shortCosts").val()) {
displayShortCosts();
}
else {
$("#btconsultsup").click(displayShortCosts);
}
});
$("#btconsultsup").click(function () {
if ($(this).val().toLowerCase() == "amend") {
$.each($('.consultsup'), function(i, item){
$(this).show();
});
$(this).val("Cancel");
} else {
$.each($('.consultsup'), function(i, item){
$(this).hide();
});
$(this).val("Amend");
}
});
|
|
|
|
|
You wouldn't need your script.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Just a little confused by this:
if ($("#shortCosts").val()) {
I have it as a class in my HTML:
|
|
|
|
|
Sorry, should be:
if ($("#amendconSupportContractCosts").val()) {
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Perfect, perfect!!!
Thank you so much sir.
Incredible!
|
|
|
|
|
program for consecutive characters which are same, omit one if consonents and do nothing if they are vowel in javascript
|
|
|
|
|
Sorry, this site does not provide code to order. Do yourself a favour and learn to do your own work. Start at: JavaScript Tutorial[^]
|
|
|
|
|
hello! I’m a beginner on coding.
I have an javascript
setInterval(function() ideally that will auto play a audio at an exact time. Now I want to use a html button to trigger the setInterval(function(). The timer works ok as I’ve tested by using an alert. Please advise if the below codes are ok to do an autoplay audio function?
thanks
the audio in HTML:
<div id="time"></div>
<audio controls style="display: none;">
<source src="audio.mp3" type="audio/mpeg" />
</audio>
the javascript:
setInterval(function() {
var d = new Date(),
hours = d.getHours(),
min = d.getMinutes(),
sec = d.getSeconds();
if (hours === 16 && min === 25 && sec === 01) {
var audio = document.getElementsByTagName('audio')[0]
audio.play()
audio.style.display = 'block'
}
}, 1000)
|
|
|
|
|
Member 15265438 wrote: Please advise if the below codes are ok to do an autoplay audio function? What happens when you try it?
|
|
|
|
|
it won’t auto-play the audio… I’ve ask someone else and said user need interact with the webpage first to play an auto-play action. So I wonder if I can use a button the trigger the javascript?
|
|
|
|
|
|
Two obvious problems here:
1) Playing audio or video except as a direct result of user interaction is generally blocked. This includes playing the audio some time after the user has clicked a button - whilst some browsers might give you a small amount of leeway, playing audio minutes after the user clicked the button is likely to be blocked.
2) Javascript timers are not guaranteed to fire at a precise time. The setInterval callback will not be called more than once per second, but there is no guarantee that it will be called at least once per second. This is particularly problematic if your page is in the background, where the timer resolution will be decreased to conserve power.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
thanks for the explaining.
So can you give any advise that would make it happen?
What I want to do sounds simple: when people go to the website and audio will auto-played at a specific time…
I understand browser will have restrictions on this but will there any ways could make it happen?
thanks
|
|
|
|
|
If you can convince the users to change their settings for your site to allow it to auto-play audio without user interaction, then you might be able to get close. You would need to change your test to see if the current time was greater than or equal to the desired time, rather than checking that it's precisely equal to the desired time. Once the desired time has passed, clear the timer to stop it from checking every second.
const playAudio = function(){
const audio = document.querySelector("audio");
audio.style.display = "block";
audio.play();
};
const shouldPlayAudio = function(){
const now = new Date();
const h = now.getHours();
if (h > 16) { return true; }
if (h < 16) { return false; }
const m = now.getMinutes();
if (m > 25) { return true; }
if (m < 25) { return false; }
const s = now.getSeconds();
return s >= 1;
};
let autoPlayTimer = null;
const playAtTime = function(){
if (shouldPlayAudio()) {
clearInterval(autoPlayTimer);
autoPlayTimer = null;
playAudio();
}
};
autoPlayTimer = setInterval(playAtTime, 1000); Demo[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
I am new to js and might be a silly question. sorry for that as I started to learn JS.
I have made 2 scripts. 1 for autoclick buttons and another 1 for autoclick nextpage button.
How to merge both codes to one after one. also I need to set 5 seconds delay between both this scripts.
Script 1:
var buttons = document.getElementsByClassName('Polaris-Button');
for(var i = 0; i <= buttons.length; i++)
buttons[i].click();
Script 2:
var btn = document.getElementsByClassName('ant-pagination-next');
for(var s = 0; s <= btn.length; s++)
btn[s].click();
Thanks in advance.
|
|
|
|
|
|
Where there are multiple CSS selectors, in what order is the result list? (I know, I should experiment and find out!). But for the OP, I suspect this may not be the required solution, since he's wanting all the buttons of one class clicked before the other. Also, it doesn't include the 5 second delay he's after.
So: simply wrap the first block of code in a function ('ClickPolaris ') and the second block in another function ('ClickPagination '). To run one, then the other, simply:
ClickPolaris();
ClickPagination();
To implement a five-second delay, you'll need a timer interval:
ClickPolaris();
setTimeout(function() {ClickPolaris();}, 5000);
Of course you can then refine it to use a single parameterised function, called twice with the different class names:
function ClickButtons(classname) {
var buttons = document.getElementsByClassName(classname);
for(var i = 0; i <= buttons.length; i++) {
buttons[i].click();
}
}
ClickButtons('Polaris-Button');
setTimeout(function() {ClickButtons('ant-pagination-next');}, 5000); and further simplify using Richard's suggested syntax:
function ClickButtons(classname) {
document.querySelectorAll(classname).forEach(btn => btn.click());
}
ClickButtons('.Polaris-Button');
setTimeout(function() {ClickButtons('.ant-pagination-next');}, 5000); Note that in this syntax you need to include the . to indicate you're selecting by class name.
|
|
|
|
|
DerekT-P wrote: Where there are multiple CSS selectors, in what order is the result list?
The matching elements will be returned in document order:
The querySelectorAll() ... must return a NodeList containing all of the matching Element nodes within the subtrees of the context node, in document order.
So given:
<input id="btn1" type="submit" class="ant-pagination-next">
<button id="btn2" class="Polaris-Button"></button>
<button id="btn3" class="ant-pagination-next"></button>
<button id="btn4" class="Polaris-Button ant-pagination-next"></button> then querySelectorAll(".Polaris-Button, .ant-pagination-next") will return [btn1, btn2, btn3, btn4] .
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks Richard - I probably ought to have known that!
|
|
|
|
|
I feel I'm pretty close here. I have like 5 phone number inputs, and instead of adding a event attribute in the input element, I thought I would just use a class called phoneNumber and loop all the element to ad an event listener. I'm close, just missing the glue to pass on the element value to the phone number function I stole off the internet and tweaked a bit.
I've done this before in typescript. But I've been really deep in this PHP project I've been working on. I can see the value of using inspector in the event, but PHP Storm isn't suggesting anything for it.
On my for loop, must examples used the index type loop, but I wanted to be a little more ES6 with it. Not sure if it's a good idea or not.
document.addEventListener("DOMContentLoaded",() => {
const phoneInputs = document.getElementsByClassName('phoneNumber');
for (phoneInput of phoneInputs) {
phoneInput.addEventListener('keydown', (e) => {
console.log(e.id);
formatPhoneNumber(e.value)
});
}
});
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
After some more reading, some articles said that this will be available within addEventListener . So I'm just going with it, should work. I have to wait for my DEV server to become available again to test it.
document.addEventListener("DOMContentLoaded",() => {
const phoneInputs = document.getElementsByClassName('phoneNumber');
for (let phoneInput of phoneInputs) {
phoneInput.addEventListener('keydown', () => {
formatPhoneNumber12(this.value)
});
}
});
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
That didn't work at all. Fiddled with it for about 30 minutes on the DEV test server this time and came up with this that works for me in this format xxx-xxx-xxxx
document.addEventListener("DOMContentLoaded",() => {
const phoneInputs = document.getElementsByClassName('form-control phoneNumber');
for (let phoneInput of phoneInputs) {
phoneInput.addEventListener('keydown', event => {
event.target.value = formatPhoneNumber12(event.target.value);
});
}
});
function formatPhoneNumber12(value) {
if (!value) return value;
const phoneNumber = value.replace(/[^\d]/g, "");
const phoneNumberLength = phoneNumber.length;
if (phoneNumberLength < 4) return phoneNumber;
if (phoneNumberLength < 7) {
return `${phoneNumber.slice(0, 3)}-${phoneNumber.slice(3)}`;
}
return `${phoneNumber.slice(0, 3)}-${phoneNumber.slice(
3,
6
)}-${phoneNumber.slice(6, 9)}`;
}
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
NB: This will only add the event handler to matching elements which exist at the point when your code is run. If you modify the document at any point to add new matching elements, they will not have the event handler attached.
You would probably be better off using event delegation[^]. Since you'd only have a single event handler, it can even be more efficient.
document.addEventListener("keydown", (e) => {
const el = e.target;
if (el.classList.contains("phoneNumber")) {
console.debug(el.id, el.value);
el.value = formatPhoneNumber(el.value);
}
});
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I wasn't thinking of that situation when I composed it. It's a customer form, with their home, office, cell phone numbers. I just needed to make sure that the user followed the format of xxx-xxx-xxxx.
So your saying that I don't have to wait until the Dom is loaded to add the keypress event?
I just thought it was the right thing to do. I'll give it a try.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|