0

I am just trying to see if one variable has a specific value and an other variable another specific value. And if so, do something.

So its simply an if statement. But it doesn't work. And i dont know why.

So i have the variable : rights and seeprices 'rights' alerts the following: "Individuell" And 'seeprices' outputs "preiseaendern".

var seeprices = $( "#seeprice").html();
var rights = $( "#rights" ).val();  //Value of Combobox

alert(seeprices + " " + rights); // seeprices outputs 'preiseaendern' and rights outputs 'Individuell'

if (rights == "Individuell" && seeprices =="preiseaendern"){
alert("it works...");
}

When i just use

if (rights == "Individuell"){
alert("it works...");
}

It works and i get the message 'it works...', but as soon as i add

&& seeprices == "preisaendern") 

It doenst work anymore, and i cant figure out why. Thank you guys in advance.

  • Try `.val().trim()` – Rajesh Aug 20 '19 at 10:01
  • Ok for starters your it should be `===` not `==` your need exact string match not truthy string match – Barkermn01 Aug 20 '19 at 10:02
  • Try some parentheses round each of those two equality tests. – peeebeee Aug 20 '19 at 10:04
  • any errors in console? – Mark van der Dam Aug 20 '19 at 10:04
  • tried it with parentheses and also wit ```===```, but its still not working. – Muhammed Basar Aug 20 '19 at 10:05
  • @Mark_Ed No none. – Muhammed Basar Aug 20 '19 at 10:05
  • 1
    You wrote "preisaendern" in your last code section and 'preiseaendern' inside your comment – Marco Aug 20 '19 at 10:06
  • Have you tried to only add **seeprices** to the condition? Or just **console.log()** **seeprices**? Are you absolutely sure that it only outputs **preiseaendern**? (You call it with HTML - that can cause some issues...) – muka.gergely Aug 20 '19 at 10:07
  • @Marco Sorry yes, but in the code it is written the right way, i copied and pasted it many times to be 100% sure that its written correctly. – Muhammed Basar Aug 20 '19 at 10:08
  • the console log exactly outputs this string 'preiseaendern' – Muhammed Basar Aug 20 '19 at 10:14
  • @MartinBarker that makes absolutely no sense - please explain how a "truthy string match" works – Jaromanda X Aug 20 '19 at 10:24
  • @JaromandaX https://www.quora.com/Is-it-better-to-compare-strings-using-or-in-Javascript so a `"5" == 5` but `"5" !== 5` – Barkermn01 Aug 20 '19 at 10:27
  • yeah sure ... but $("someinput").val() **IS** a string and "whatever you put here" **IS** a string ... so ... likelihood of coercion issue === 0 :D – Jaromanda X Aug 20 '19 at 10:30
  • @JaromandaX because .val() does not always return a string HTML could get it to return a Number or Array https://api.jquery.com/val/ – Barkermn01 Aug 20 '19 at 10:32
  • @MartinBarker - since the input in the question is an input that accepts text, I doubt it - and `$.html()` will definitely return a strings, so I doubt the OP **should use === instead of ==** in any way fixes this problem – Jaromanda X Aug 20 '19 at 10:37
  • @JaromandaX it's not a fix for his problem that's why it's a comment and not an answer and why my answer does not say to use it as a fix it just uses it, it's good practice to always use `===` by default and only use `==` when you know your value will only match on truthy, not truth and even then I would say your code is authored incorrectly and you should covert the type before comparison. the same as any other modern language and bable also reports this as bad practice and gives a warning. – Barkermn01 Aug 20 '19 at 10:44
  • So i tried making a JSFiddle to show what this code should do. jsfiddle.net/2ayz67h3/3 On my Local code actually it works when i change the first two Options in the select. In this jsfiddle example none seems to work. And this gif shows how it actually works with the first two select options, but not with the third one. ('Individuell') https://media.giphy.com/media/j5ypeTGJDi6Fb97R8V/giphy.gif – Muhammed Basar Aug 20 '19 at 10:54

1 Answers1

1

You should always test your check with hardcoded values before using user values https://jsfiddle.net/czw72sj1/

var seeprices = "preiseaendern";
var rights =  "Individuell"; //Value of Combobox

alert(seeprices + " " + rights); // seeprices outputs 'preiseaendern' and rights outputs 'Individuell'

if (rights === "Individuell" && seeprices === "preiseaendern"){
alert("it works...");
}

When using that it works correctly, so this means it's your user input that is not working 100% so as @Rajesh said to use String.trim()

So 2 things stand out at me one your getting text out of HTML while allowing HTML codes, $( "#seeprice").html(); should be $( "#seeprice").text(); as this will strip any HTML or spacing arround the text your need but it should still be .trim()ed

var seeprices = $( "#seeprice").text().trim();
var rights = $( "#rights" ).val().trim(); 

If this still does not work as it might not i'm guessing because i can't see your HTML create a JSFiddle with the #seeprice element and the #rights Combobox so we can see what's going wrong with reading the DOM values.

Barkermn01
  • 6,781
  • 33
  • 83
  • So i tried making a JSFiddle to show what this code should do. https://jsfiddle.net/2ayz67h3/3/ On my Local code actually it works when i change the first two Options in the select. In this jsfiddle example none seems to work. – Muhammed Basar Aug 20 '19 at 10:45
  • And this gif shows how it works with the first two options https://media.giphy.com/media/j5ypeTGJDi6Fb97R8V/giphy.gif – Muhammed Basar Aug 20 '19 at 10:52
  • 1
    I got it now thank you really much. It was the ```.text().trim();```which i needed :) – Muhammed Basar Aug 20 '19 at 10:59