Is there a way to change the text of certain paragraphs by clicking different buttons without having to rewrite the entire function every time for just two text elements?
Here is the original code with no parameters:
$('.ilBorgo').click(good);
function good() {
if ($('#page').hasClass('content') === false) {
$('#page').addClass('content');
$('.title').text("text1");
$('.text').text("text2");
$('#slider').removeAttr("id");
$('.container').css("background-image", 'url' + imgPath[0]);
} else {
$('#page').removeClass('content');
$('.container').attr("id", "slider");
$('.title').text("");
$('.text').text("");
};
document.getElementById("demo").innerHTML = "it's running!";
};
However, when I tried to add parameters to improve the structure of the code, the event triggered automatically without any click after page load.
When trying to click on it, nothing happens as expected and the text section does not disappear like it should.
Below is the code with added parameters:
$('.ilBorgo').click(good("hello", "world"));
function good(a, b) {
if ($('#page').hasClass('content') === false) {
$('#page').addClass('content');
$('.title').text(a);
$('.text').text(b);
$('#slider').removeAttr("id");
$('.container').css("background-image", 'url' + imgPath[0]);
} else {
$('#page').removeClass('content');
$('.container').attr("id", "slider");
$('.title').text("");
$('.text').text("");
};
document.getElementById("demo").innerHTML = "it's running!";
};