Hi! I just started learning Javascript & while practicing I got two problems I couldn't resolve. The 1st is about this code: if (name!=null && name!="") { document.write("whatever") } Code (markup): How can I add to it different actions for different input values? Can I for example change name!=null && name!="" Code (markup): to & add a code for doing nothing between { } ? I tried to leave it blank but it didn't work If I could do that I would be able then to add if ... else for different input values entered in the prompt box. --------------- My second question is about the input values, in my tests i found they are case sensitive. Is there a way to remove that? I mean I want when users type in the prompt box digitalpoint, DigtalPoint, dIgitAlpOinT, etc they get the same action executed. I tried my best to explain, I hope you got my point ... thanks in advance!
JavaScript is a case sensitive language; there's no way to change that. But here's what I do if I want to effect case-insensitive validation, where inputString is the string coming from the prompt box: if ( inputString.toLowerCase() == "digitalpoint" ) { do stuff } Code (markup): The trick here is the toLowerCase method - check it out at w3schools.com/jsref/jsref_toLowerCase.asp.
Nice! One problem solved! Thanks buddy!! I added to your reputation Any help would be appreciated for my other problem, thanks!
No problem, and thanks for the props. I was thinking about your first question, and there are a couple of things to know that might help you out. First off, && indicates AND. So if ( color == "blue" && color == "red" ) Code (markup): will never evaluate to true, because color can't == both "blue" and "red". But if ( color != "blue" && color != "red" ) Code (markup): will sometimes be true, for instance if color == "green". Second, when I want to 'do nothing', I just put some comments inside the { }: if ( color != "blue" && color != "red" ) { /* Do nothing */ } else { alert("Could be green!"); } Code (markup): I apologize in advance if any of my syntax is wrong - I was just typing into the comments box here.
Thanks! What I did is I changed the 'and' with 'or' & it worked! So instead of: if (name==null [COLOR="Red"]&&[/COLOR] name=="") { } else if Code (markup): I put: if (name==null [COLOR="Red"]||[/COLOR] name=="") { } else if Code (markup): No it's just working fine, thanks again for help
Not too shabby. When I was first learning JavaScript, I made good traction by using the JavaScript tutorials on HTML Goodies. (htmlgoodies.com/primers/jsp/article.php/3478531) The material's not modernized (it doesn't talk about Ajax or unobtrusive JS), but it does give you a good idea of what JavaScript can and can't do.
thanks for link, here's a clickable version for those who may need it: http://htmlgoodies.com/primers/jsp/article.php/3478531