1
I have an interface where I have several buttons and each one represents an instrument.
When I press, to select the instrument I want, I have to change the library variable I’m using.
Something like: Loads piano (html) knob and changes in js the var selectedPreset = (corresponding instrument number).
I can detect which buttons are being clicked but I can’t change the variable through the function. If you Return directly from the piano, it works, but inside the if, it doesn’t work.
<div class="Instrumentos" >
<button class="fas fa-play" name="piano" ></button>
<button class="fas fa-play" name="guitar" ></button>
var buttons = document.getElementsByTagName("button");
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", message);
}
function message(som) {
var piano = _tone_0000_SBLive_sf2
var guitar = _tone_0240_SBLive_sf2
var name = this.name;
var som
if (name == "piano") {
som = piano
console.log(som)
return som
//alert("Piano");
} else if (name == "guitar") {
som = guitar
alert("Guitarra");
}
return som
}
var selectedPreset = message()
Your
selectedPreset
performs the functionmessage()
as soon as Javascript is loaded, and not at the click of the button, so you cannot "change the value of the variable". Another detail is that thefunction message(som)
receives a parameter that does not use...– Rafael Tavares
but @Rafaeltavares, and the line
buttons[i].addEventListener("click", message);
linking the click event to the fuctionmessage
?– Ricardo Pontual
@Ricardopunctual did not understand. This line will work, but still will not change the value of
selectedPreset
, since he is out of functionmessage
.– Rafael Tavares
the problem I saw with this piece of code are the variables
_tone_0000_SBLive_sf2
and_tone_0240_SBLive_sf2
that are not defined... your code can have several improvements, such as the variablesom
that @Rafaeltavares has already mentioned, that is not being passed to Function and, seeing me, does nothing useful in Function, is this variable that mentioned in the question that wants to change?– Ricardo Pontual
@Rafaeltavares what I meant is, about your comment "execute the message() function as soon as Javascript is loaded, not at the click of the button", when you click the function is executed in it, it is not well explained, that’s it. click tbm is executed
– Ricardo Pontual
What I want to do is that each button has one of those variables associated with tone, because that’s what makes selectedPreset work. If you press the piano button, the variable had to change to _tone_0000_SBLive_sf2, if it was on the guitar, it changed to _tone_0240_SBLive_sf2 ....
– CParauta
Where is
return som
change toselectedPreset = som
– Augusto Vasques
@Thank you very much... As I said I have little experience and this detail obviously made all the difference... Thank you!
– CParauta
I’m supposed to erase the question?
– CParauta
Leave open maybe a younger user want to take advantage of my comment and submit a response to gain reputation. Or a more experienced user want to take advantage of the question to guide about the programming style. I have no interest in answering, because if I had.
– Augusto Vasques