Millefeuille
Millefeuille

Reputation: 51

How to alert a special thing according to what is checked in Javascript

if (map == "checked") { 
    alert("map ok")
}
if (joueurs == "ckecked") {     
        alert("joueurs ok")
}
if (point == "checked") {   
        alert("points ok")
}

Don't need to explain the actual output and I know all the if statements are totally wrong. I just wanted to show it as a clear example.

I would like the output to be like : "map player point ok" if all three a checked, "player map ok" if only player and map are checked, only "player ok" if only "player" is checked...

Ask if you want HTML (don't really thinks it is necessary)

Problem is partially resolved :

    if ((document.getElementById("joueurs").checked == true)) {

        alertContent = alertContent + nbjoueur + " Joueur(s), dont " + $bot.length + " bot(s) | "
    }

if ((document.getElementById("map").checked == true)) {

    alertContent = alertContent + "Map : " + map + " | "
}

 if ((document.getElementById("points").checked == true)) {
    var pointin=$.trim($("#pointsinput").val());
    if($("#pointsinput").val().length > 0) {

    alertContent = alertContent + "Partie a " + points + " point(s) |"
    }
    else {
alert("Le champ points n'est pas rempli !");
return;
    }
    alert(alertContent);
window.open('mailto:[email protected]?subject=Formulaire Quake&body=' + alertContent);

}

The problem is that the mail body is empty, but alert return the correct text...

Upvotes: 1

Views: 39

Answers (2)

Aron
Aron

Reputation: 3935

Another approach would be to use an array and join the array with spaces to generate the message:

var items = [];

if (map == "checked") { 
    items.push("map");
    alert("map ok")
}
if (joueurs == "ckecked") { 
    items.push("player");    
    alert("joueurs ok")
}
if (point == "checked") {
    items.push("point");
    alert("points ok")
}

if(items.length > 0){
    var message = items.join(" ") + "ok";
    alert(message);     
}

And another option would be a mask variable:

var mask = 0;

if (map == "checked") { 
    mask += 1;
    alert("map ok");
}
if (joueurs == "ckecked") { 
    mask += 2;    
    alert("joueurs ok");
}
if (point == "checked") {
    mask +=4;
    alert("points ok");
}

if(mask == 7){
    alert("map player point ok");
}
else if (mask == 3){
    alert("player map ok");
}
else if (mask == 1){
    alert("player ok");
}

Upvotes: 1

Wayne Allen
Wayne Allen

Reputation: 1745

You need to build the output before you display it. So, in the example you have provided:

var alertContent = "";
if (map == "checked") { 
    alertContent = "map";
}
if (joueurs == "checked") { 
    alertContent = alertContent + (alertContent) ? " " : "" + "joueurs";
}
if (point == "checked") { 
    alertContent = alertContent + (alertContent) ? " " : "" + "points";
}
if (alertContent) alert(alertContent + " ok");

The only complexity is adding spaces between the words where required - which is what the (alertContent) ? " " : "" statements do.

Upvotes: 0

Related Questions