How to change button with tag <h2>
I have a working random song generator that has a button for playing and pausing, as well as a button for randomizing songs. I do this using variables randand s, this is not the best way, but it works. I want the disclosure button to show that the song is pressed when it is pressed, but I don’t know how this happens, can anyone help?
HTML:
<audio id="1st" >
<source src="intervals/1st.mp3" type = "audio/mp3"/>
</audio>
<audio id="2nd" >
<source src="intervals/2nd.mp3" type = "audio/mp3"/>
</audio>
<audio id="3rd">
<source src="intervals/3rd.mp3" type= "audio/mp3"/>
</audio>
<audio id="4th">
<source src="intervals/4th.mp3" type= "audio/mp3"/>
</audio>
<audio id="5th">
<source src="intervals/5th.mp3" type= "audio/mp3"/>
</audio>
<audio id="6th">
<source src="intervals/6th.mp3" type="audio/mp3" />
</audio>
<audio id="7th">
<source src="intervals/7th.mp3" type="audio/mp3" />
</audio>
<button type="button" id="reveal">Reveal</button>
<button type="button" id="playButton"></button>
<button type="button" id="randomise"></button>
JavaScript:
var playButtonJS = document.getElementById('playButton');
var randomiseJS = document.getElementById('randomise');
var revealJS = document.getElementById("reveal");
var FstJS = document.getElementById('1st');
var SndJS = document.getElementById('2nd');
var TrdJS = document.getElementById('3rd');
var FOthJS = document.getElementById('4th');
var FIthJS = document.getElementById('5th');
var SIthJS = document.getElementById('6th');
var SEthJS = document.getElementById('7th');
var audioPlaying = [FstJS, SndJS, TrdJS,FOthJS, FIthJS, SIthJS, SEthJS];
var rand = audioPlaying[Math.floor(Math.random() * audioPlaying.length)];
playButtonJS.addEventListener('click', playOrPause, false);
randomiseJS.addEventListener('click', random, false);
revealJS.addEventListener('click', rev, false);
function random() {
rand.pause();
playButtonJS.style.backgroundImage = 'url(playbutton.svg)';
var song = audioPlaying[Math.floor(Math.random() * audioPlaying.length)];
playButtonJS.style.backgroundImage = 'url(pausebutton.png)';
songf(song);
}
function songf(s) {
rand.currentTime=0;
rand = s;
if (!s.paused && !s.ended) {
s.pause();
playButtonJS.style.backgroundImage = 'url(playbutton.svg)';
}
else {
s.play();
playButtonJS.style.backgroundImage = 'url(pausebutton.png)';
}
}
function playOrPause() {
if (!rand.paused && !rand.ended) {
rand.pause();
playButtonJS.style.backgroundImage = 'url(playbutton.svg)';
}
else {
rand.play();
playButtonJS.style.backgroundImage = 'url(pausebutton.png)';
}
}
JQuery solution:
rand , . "selectedindex". ? - , .
, .
, <h2 id="name1">Name of the song</h2>...<h2 id ="name6">... .
: ;
,
-
function reveal () {
$("#name+rand").style.display = "block";
console.log('You are playing the song number', rand)
}
/div/h 2 , HTML :
$("#label").html('Song is the number: '+ selectedIndex);
JQuery:
document.getElementById("label").innerHTML = "Song is number" +
String(selectedIndex);
, .
$("#label").html('Song is:'+ ArrayOfNames[selectedIndex]);
JQuery:
document.getElementById("label").innerHTML = "Song is" +
ArrayOfNames[selectedIndex];
: JQuery, .
, . th "songTitle" html .
document.getElementById("songTitle").innerHTML = "New song name";
https://www.w3schools.com/jsref/prop_html_innerhtml.asp
, .:)
var songNames = ["songName_1", "songName_2", "songName_3", "songName_4"];
function randomSong() {
var songName = arguments[0][randomNum(0, songNames.length - 1)];
document.getElementById("songName").innerHTML = songName;
}
document.getElementById("randomSong").addEventListener("click", function() {
randomSong(songNames);
})
function randomNum(minNum, maxNum) {
switch (arguments.length) {
case 1:
return parseInt(Math.random() * minNum + 1, 10);
break;
case 2:
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
break;
default:
return 0;
break;
}
}<h2 id="songName"></h2>
<input id="randomSong" type="button" value="randomSong!" />This is what I understand the meaning of brother, your code is really a bit dirty if there are problems you can ask me.
I have a better solution, but now I am at work and then sent to you.