I wrote this function for a plug.dj script, it adds a chat message if the answer is yes, but I'm having trouble to end the function if the answer is no. I am not a pro programmer.
Code:
f_votelggr: function (obj){
if (nScript.mehShow) {
if (obj.vote != 1) {
prompt(obj.user.username + " didn't liked this song! Do you want to alert the users?")
if ("yes");
API.sendChat(obj.user.username + "didn't like this song!");
} else {
return
};
},
Try this:
if ("no"){
return null;
}
I guess this is what you are looking for. It will leave the function.
Edit:
var input = prompt(obj.user.username + " didn't liked this song! Do you want
to alert the users?")
if (input == "yes"){
API.sendChat(obj.user.username + "didn't like this song!");
}
else if{
(input == "no"){
// do stuff here
return null;
} else {
return}
};
You should receive the prompted input from user
var input = prompt(obj.user.username + " didn't liked this song! Do you want
to alert the users?")
if (input == "yes"){
API.sendChat(obj.user.username + "didn't like this song!");
}
else{
return;
};
And also you have
if ("yes");
Which terminates your if there right away. Remove that ; and use {} just to avoid confusion and bugs.
var input = prompt(obj.user.username + " didn't liked this song! Do you want to alert the users?");
input = input.toLowercase();
if (input === "yes") {
API.sendChat(obj.user.username + "didn't like this song!");
} else {
return false;
};
Related
var confirmation = confirm("Are you sure you want to enter your name?");
function confirmTrue() {
var nameInput = prompt("Great! Now enter your name:");
return nameInput;
}
if (confirmation == true) {
confirmTrue();
} else {
alert("I was hoping for a response..");
}
if (confirmTrue != null || confirmTrue != "") {
alert("Hello, " + window.nameInput);
}
}
confirmCallName();
i want to make a function where you confirm something to type your name in, and then after the input, it will greet you with the input result. but when the prompt returns true or false, there is a final alert that says "Hello, undefined". how do I make it's so that it doesnt activate when we cancel the prompt and after I confirm the input?
someone told me that the variable is inside the function. if so, how do I make it global?
I made a couple of modifications to get this working how I think you want it. The code is below. You need to assign the value of the confirmTrue result to a variable and then test that. Also, you need to add the case to handle cancelling that result.
var confirmation = confirm("Are you sure you want to enter your name?");
function confirmTrue() {
var nameInput = prompt("Great! Now enter your name:");
return nameInput;
}
if (confirmation == true) {
var getInput = confirmTrue();
if (getInput) {
alert("Hello, " + getInput);
} else {
alert("You cancelled your input.");
}
} else {
alert("I was hoping for a response..");
}
Okay so I technically have everything working in the program, but I need to permanently change the values of the array that I create in PROGRAM 2 and add -blip and -clang. What do I call upon to change the values so that I can call them to the console in PROGRAM 3? I can't have anything in the body so innerHTML and document.write are out of the question(which sucks). Problem area is marked with a comment on line 37 to make it easier to find.
<!DOCTYPE html>
<html>
<head>
<title>Project 1 – Michael Fiorello</title>
<p id="demo"></p>
<script>
do {
//MAIN MENU
var input = prompt("Please enter 1, 2, 3, or exit.");
{
//PROGRAM 1-Enter the string to be converted to robot speak
if (input === "1")
do {
var one = prompt ("Please enter a string.");
{
if (one === "")
{
console.warn("You need to enter something");
}
}
} while (one === "")//keep repeating program 1 until something is entered, aka cannot be blank.
//PROGRAM 2-Convert the string into robot speak
else if (input === "2")
{
if (one == null) {
console.warn ("You need to first enter a String");
}
else
{
console.log ("String Converted")
var res = one.split(" ");
for(i = 0; i<res.length; i++)
if(res[i].length >= 5)
//What do I do here to change the value of entered strings in the array, rather than just write it out?
{
document.write(res[i]+"-blip ");
}
else
{
document.write(res[i]+"-clang ");
}
}
}
//Program 3 Robot Language version of the string will appear in the console
else if (input === "3")
{
var output = res.join(" ");
alert ("AWESOME!");
console.log (output);
}
else if (input == null|| input.toLowerCase() == "exit")
{
alert ("Thanks for using the ROBOT Language Converter!");
}
else
{
alert ("Nope");
console.warn("You need to enter something");
}
}
} while(input.toLowerCase() != "exit");
</script>
</head>
</html>
It was so simple that I feel like an idiot. All I had to do was change the document.write lines to
res[i] = (res[i]+"-blip ");
and it changes the values to what I need.
I have some code that saves the users input into local storage and then prints the value into the selected ID.
function saveData() {
var input = document.getElementById("saveServer");
localStorage.setItem("server", input.value);
console.log(localStorage.getItem("server"));
document.getElementById("yourname").innerHTML ="Okay " + (localStorage.getItem("server")) + " Let's get started! Push the button on the right to begin the story!" ;
}
How do I make it so if the input field is empty a statement is produced informing the user to input their name.
I've had a look at if and else statements but anything I try doesn't seem to work.
Edit:
Figured it out!
if (saveServer.value == "") {
document.getElementById("yourname").innerHTML = "Please input your name";
}
else {
document.getElementById("yourname").innerHTML ="Okay " + (localStorage.getItem("server")) + " Let's get started! Push the button on the right to begin the story!" ;
}
Add a condition to check if input not empty.
function saveData() {
var input = document.getElementById("saveServer").value;
if(input ==="") alert("Enter saveServer");
else{
localStorage.setItem("server", input.value);
console.log(localStorage.getItem("server"));
document.getElementById("yourname").innerHTML ="Okay " + (localStorage.getItem("server")) + " Let's get started! Push the button on the right to begin the story!" ;
}
}
Like this:
function saveData() {
var input = document.getElementById("saveServer");
localStorage.setItem("server", input.value);
var serverValue = localStorage.getItem("server");
if (!serverValue) {
document.getElementById("error").innerHTML = "Oh dear! Nothing was found";
}
document.getElementById("yourname").innerHTML = "Okay " + (localStorage.getItem("server")) + " Let's get started! Push the button on the right to begin the story!";
}
Fiddle: http://jsfiddle.net/KyleMuir/LhyXr/
I have an #error paragraph. Everytime there is an error within the form on submit. The inputs placeholder text gets added to the #error paragraph.
My problem:
It happens everytime a user clicks submit. So the #error message returns:
Please fill in yourfirst name, last name, company, position, first
name, last name, company, position, first name, last name, company,
position, first name, last name, company, position, first name, last
name, company, position, first name, last name, company, position,
I've looked for other solutions and tried this:
if (input.attr('placeholder').indexOf($('#error')) >= 0){
} else{
$('#error').append(input.attr('placeholder').toLowerCase() + ', ');
}
Is there any way to check if the placeholder text already exists in the #error message? Here's a fiddle. I'm sorry it's so convoluted. But its what i've been working on and had it handy.
Thanks for your help in advance!
http://jsfiddle.net/8YgNT/20/
var errorText = '';
//Validate required fields
for (i = 0; i < required.length; i++) {
var input = $('#' + required[i]);
if ((input.val() == "") || (input.val() == emptyerror)) {
input.addClass("tobefixed");
errornotice.fadeIn(750);
if (input.attr('placeholder').indexOf($('#error')) >= 0) {
// do nothing
} else {
errorText = errorText + $(input).attr('placeholder').toLowerCase() + ', ';
}
input.val(emptyerror);
errornotice.fadeIn(750);
} else {
input.removeClass("tobefixed");
}
}
$('#error').html('').append('Please fill in your ' + errorText);
I simple add one line in your fiddle and it's working now:
required = ["id_first_name", "id_last_name", "id_firmbox", "id_job_title"];
errornotice = $("#error");
emptyerror = "Please fill out this field.";
$("#startform").submit(function(){
//Validate required fields
$("#error").html("Please fill in your");
for (i=0;i<required.length;i++) {
var input = $('#'+required[i]);
if ((input.val() == "") || (input.val() == emptyerror)) {
input.addClass("tobefixed");
errornotice.fadeIn(750);
// =====Here===== //
if (input.attr('placeholder').indexOf($('#error')) >= 0){
} else{
$('#error').append(input.attr('placeholder').toLowerCase() + ', ');
}
// ============== //
input.val(emptyerror);
errornotice.fadeIn(750);
} else {
input.removeClass("tobefixed");
}
}
if ($(":input").hasClass("tobefixed")) {
return false;
} else {
errornotice.hide();
return true;
}
});
$(":input").focus(function(){
if ($(this).hasClass("tobefixed") ) {
$(this).val("");
$(this).removeClass("tobefixed");
}
});
This line do the all trick:
$("#error").html("Please fill in your");
Saludos ;)
If you want to check whether #error contains the string you're wondering about, you can use
($('#error').text().indexOf(a_string)) != -1
This will return true if #error contains a_string.
There's a longer discussion on searching strings for other strings in this question.
It seems you're doing it the wrong way round (and forgetting to get the text out of the #error element):
if ( $('#error').text().indexOf( input.attr('placeholder') ) >= 0)
i used to use jquery validation plugin but because of lack of this plugin with wysiwyg plugins i wrote a simple script to validate my form
i tried to do it like this
function validateArticle(formData, jqForm, options) {
$('#errors').empty();
if ($('#editor').val() == 0) {
$('#errors').show();
$('#errors').append('<li>please enter your article body</li>');
return false;
}
if ($('#ArticleTitle').val() == 0) {
$('#errors').show();
$('#errors').append('<li>please enter your article title</li>');
return false;
}
$('#errors').hide();
return true ;
}
i found to 1 problem when it validate the form it's validating it field by field so the errors messages doesn't appear at once
i tried to do something like
var errors = [];
function validateArticle(formData, jqForm, options) {
$('#errors').empty();
if ($('#editor').val() == 0) {
errors.push('<li>please enter your article body</li>');
var invalid = 1 ;
return false;
}
if ($('#ArticleTitle').val() == 0) {
errors.push('<li>please enter your article title</li>');
var invalid = 1 ;
return false;
}
if(invalid == 1){
$.each(errors , function(i, val) {
$('#errors').append(errors [i]);
});
}
$('#errors').hide();
return true ;
}
i tried to push errors as array elements and loop through them in case of invalid is true
bu this one doesn't work at it all ?
is there any way to make it work ?
if ($('#editor').val() == 0) // This is checking if value is 0
This does not make sense..
Try
if ($('#editor').val() == '') //Instead check for empty string
EDIT
Also you seem to be hiding the error's div in the end.
$('#errors').hide();
Try this code Instead
$('#validate').on('click', function() {
var errors = [];
var html = '<ul>' ;
valid = true;
$('#errors').empty();
if ($('#editor').val() == '') {
errors.push('<li>please enter your article body</li>');
valid = false;
}
if ($('#ArticleTitle').val() == '') {
errors.push('<li>please enter your article title</li>');
valid = false;
}
if (!valid) {
html += errors.join('') + '</ul>'
$('#errors').append(html);
}
else{
$('#errors').hide();
}
return valid;
});
DEMO