Jump to content

Straw

Members
  • Content Count

    5
  • Joined

  • Last visited

Community Reputation

0 Neutral

About Straw

  • Rank
    Newbie
  1. Straw

    Problem with array

    Well...who am I to argue but the thing is it really makes a difference. At least in the example am using. When am setting the background color it does not matter, I can even use "yellow" and it works. But when I use the comparison operator (!=), to check the background color it does not work if I do not use the spaces.
  2. Straw

    Problem with array

    Thank you dsonesuk... Just minutes after I posted my last message: I GOT IT. When comparing color as a string you must use "rgb(xxx, xxx, xxx)". It does not work if you use "#ffff00" and it does not work if you use "yellow" either. And when you use "rgb(xxx, xxx, xxx)" make sure that you put a space after each comma otherwise it does not work either.
  3. Straw

    Problem with array

    Hey again.. I'm sorry but I still don't know how to solve this problem. Thanks for your answer though. I thought I could explain my problem bit by bit and maybe that could help me better understand. So here goes.. Please look at the code beneath and tell me why, when I hit the save-button a second time after inserting a value into the first input field, the console keeps showing the statement "a value has been added..". The second time it should show "this input field has already been used.." What I want my program to do is: 1. Tell me that a value has been added to a certain input field... 2. ...or tell me that the same input field has not been used yet... 3. ...or tell me that the same input field has already got a value earlier. As you can see I set a yellow background-color to the input field after adding a value to it. <!DOCTYPE html> <html> <head> <style> input { width: 50px; height: 50px; font-size: 40px; text-align: center; } </style> </head> <body> <input type="text"> <input type="text"> <input type="text"> <button id="save">save</button> <script src="jquery-3.3.1.min.js"></script> <script> var input = $("input:first-child"); $("#save").on("click", function() { if(input.val() != "" && input.css("background-color") != "rgb(255,255,0)") { console.log("a value has been added.."); input.css("background-color","yellow"); } else if(input.val() === "") { console.log("no value yet.."); } else { console.log("this input field has already been used.."); } }); </script> </body> </html>
  4. Straw

    Problem with array

    The problem I have is that when I hit the event-button it keeps adding the same input value to the array every time. Let's say I use the second input field and put the number 3 there. I then click the button and the program adds a "3" to my array. If I then click the button again it adds another "3" to the array. That is not supposed to happen. I was hoping my program could keep track of which input field that's been used.
  5. Hey guys... I'm making a program where I want to store some values in an array. The values are taken from a couple of input fields. I use the input fields randomly (not in order). But I want to store these values in order in the array. I thought I could do this by checking if a certain input field is filled. And to make sure the value from that field did not get added to the array every time I hit the event-button I changed the background color of the input field to check if I already had added that value to the array. <script> var x = 0; var array = []; $("button").on("click", function() { console.log("Before IF: " + $("input:eq(0)").val() + " - " + $("input:eq(0)").css("background-color")); if($("input:eq(0)").val() != "" && $("input:eq(0)").css("background-color") != "rgb(255,255,0)") { array[x] = $("input:eq(0)").val(); x++; $("input:eq(0)").css("background-color","rgb(255,255,0)"); } else if($("input:eq(1)").val() != "" && $("input:eq(1)").css("background-color") != "rgb(255,255,0)") { array[x] = $("input:eq(1)").val(); x++; $("input:eq(1)").css("background-color","rgb(255,255,0)"); } else if($("input:eq(2)").val() != "" && $("input:eq(2)").css("background-color") != "rgb(255,255,0)") { array[x] = $("input:eq(2)").val(); x++; $("input:eq(2)").css("background-color","rgb(255,255,0)"); } console.log("After IF: " + $("input:eq(0)").val() + " - " + $("input:eq(0)").css("background-color")); console.log(array); console.log("---------------------------------"); }); </script>
×
×
  • Create New...