Jump to content

Shinori

Members
  • Content count

    8
  • Joined

  • Last visited

Community Reputation

0 Neutral

About Shinori

  • Rank
    Newbie
  1. isEmpty() || === '' || === null -> not working?

    Well I replied those if-conditions. Still no change. In my original code im checking if string is empty before posting http data to another site via AJAX. Could AJAX cause problems? See code above. Thanks for your patience and endruance! :-)
  2. isEmpty() || === '' || === null -> not working?

    I'm sorry. Meant to say the following: "If input (id="vname) is empty i only get a green 'foo'" I want to check if the string in input is empty if so print success else print errormsg. Checking for empty string isnt't working. vname.isEmpty() vname === '' vname.length === 0 vname === null and reversed operators do not work. This is the entire code. I'm trying to use it with AJAX (without jquery): nname, email, bday and msg could also be empty. But its not even working with vname. <script> function send_mail() { var vname = document.getElementById('vname').value; var nname = document.getElementById('nname').value; var email = document.getElementById('email').value; var bday = document.getElementById('bday').value; var msg = document.getElementById('textkontakt').value; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById('wrapper-contact').innerHTML = this.responseText; } }; if (vname.length === 0 || vname === '' || vname == null) { document.getElementById('contact-response').style.color = 'red'; document.getElementById('contact-response').innerHTML = "Eine oder mehrere Felder sind nicht ausgefüllt<br> Anfrage konnte nicht gesendet werden!"; } else { xmlhttp.open('POST', "sendmail.php?vname=" + vname + "&nname=" + nname + "&email=" + email + "&bday=" + bday + "&msg=" + msg, true); xmlhttp.send(); } } </script> HTML: <body> <div id="wrapper-contact"> <img src="\ressource\images\nydoo-title-trans.png" class="logo-small" title="Nydoo Logo (trans)" alt="Usually there should be an image here. It may be an error but could<br>also be down to a slow internet connection or a plugin installed on your browser!" id="image-title"> <form action=""> <p class="nydoo-regular font-size-12 color-white text-align-left">Es kann bis zu 2 Werktage dauern,<br> bis wir Ihre Nachricht bearbeiten<br>Sollten Sie schneller eine Antwort benötigen,<br> kontaktieren Sie uns unter +49 1573 2695219</p> <input class="transform-x-50 margin-left-50percent margin-top-30 textinsert-large" type="text" id="vname" placeholder="*Vorname" required> <input class="transform-x-50 margin-left-50percent margin-top-5 textinsert-large" type="text" id="nname" placeholder="*Nachname" required> <input class="transform-x-50 margin-left-50percent margin-top-5 textinsert-large" type="email" id="email" placeholder="*Email" required> <input class="transform-x-50 margin-left-50percent margin-top-5 textinsert-large" type="date" id="bday"> <textarea id="textkontakt" class="transform-x-50 margin-left-50percent margin-top-5 textinsert-large" placeholder="*Nachricht" style="height:150px" required></textarea> <input class="transform-x-50 margin-left-50percent margin-top-10 button-submit-medium" onclick="send_mail()" type="button" value="Senden"> <p class="nydoo-regular font-size-12 color-white text-align-left">*Erforderliches Feld</p> <p id="contact-response" class="nydoo-regular font-size-12 color-white text-align-left"></p> </form> </div> </body>
  3. Dear W3schools community, I want to check if a required input is empty (has more then 0 characters). You guys have any idea what I'm doing wrong? Thanks in advance! If there is no character in input (id="vname") I still get a red "bar" Here's my code: function foobar() { var vname = document.getElementById('vname').value; if (vname.length > 0 && vname != '' && vname != null) { document.getElementById('contact-response').style.color = 'green'; document.getElementById('contact-response').innerHTML = "foo"; } else { document.getElementById('contact-response').style.color = 'red'; document.getElementById('contact-response').innerHTML = "bar"; } } I also tried length, null and '' separately. I also tried doing it with isEmpty(). Also I tried using || instead of &&. Nothing worked yet. Heres my HTML: <div id="wrapper-contact"> <form> <input type="text" id="vname" placeholder="*Vname" required="yes"> <input type="button" onclick="foobar()" value="Senden"> <p id="contact-response"></p> </form> </div>
  4. [Solved] Trouble with changing img src (HTML/JS)

    Eventually the time between creating and XMLhttpRequest and getting something back was the reason that class hasnt been changed correctly. Not even sure why i wrote it into the readystate-condition Thanks anyways. You must be irritated by such newbies like me
  5. [Solved] Trouble with changing img src (HTML/JS)

    Okay. so I changed it. Still haviing the same problems: BTW if I click id'info' twice. The img changed to black. Is there a timing problem ? Code: <!DOCTYPE html> <html lang='de'> <head> <script type='text/javascript'> function change_content(contentname) { if (document.getElementById(contentname).className == 'notactive') { document.getElementById('login').className = 'notactive'; document.getElementById('news').className = 'notactive'; document.getElementById('info').className = 'notactive'; document.getElementById('websites').className = 'notactive'; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById('content').innerHTML = this.responseText; if (contentname == 'kontakt' || contentname == 'hilfe' || contentname == 'impressum') { document.getElementById('info').className = 'active'; } else { document.getElementById(contentname).className = 'active'; } } }; xmlhttp.open('POST', contentname + 'page.php', true); xmlhttp.send(); } if (document.getElementById('info').className == 'active') { document.getElementById('dropdown-icon').src = '/ressource/images/nydoo-dropdown-icon-black.png'; } else { document.getElementById('dropdown-icon').src = '/ressource/images/nydoo-dropdown-icon-yellow.png'; } } window.onload = change_content; </script> </head> <body onload='change_content('login'); return false;'> <header> <ul id='mainnavigation'> <li id='mainlist' class='dropdown'><a id='info' class='notactive' onclick='change_content(this.id);'>Information<img id='dropdown-icon' style='position: absolute; width: 15px; height: 12px; margin-right: 20px;' src='/ressource/images/nydoo-dropdown-icon-yellow.png'></a> </li> </ul> </header> </body> </html>
  6. [Solved] Trouble with changing img src (HTML/JS)

    Okay, I understood the "body" part. But what do you mean by multiple reference of the same element? Am I only allowed to use document.getElementById() once for each element? Sorry if my english isn't appropriate. Not my native language :/ Thanks a lot!
  7. [Solved] Trouble with changing img src (HTML/JS)

    Alright. Thanks! I changed it to the logical Operator '==' . Still doesn't it solve my problem :/ The img src keeps being yellow all the time.
  8. Dear W3Schools-Community, There is something not working with my code right here. It should be mentioned that I am fairly new to using JavaScript. Here is my HTML code (stripped down to the essential parts): <!DOCTYPE html> <html lang="de"> <head> <script type="text/javascript"> function change_content(contentname) { if (document.getElementById(contentname).className = 'notactive') { document.getElementById('login').className = 'notactive'; document.getElementById('news').className = 'notactive'; document.getElementById('info').className = 'notactive'; document.getElementById('websites').className = 'notactive'; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById('content').innerHTML = this.responseText; if (contentname == 'kontakt' || contentname == 'hilfe' || contentname == 'impressum') { document.getElementById('info').className = 'active'; } else { document.getElementById(contentname).className = 'active'; } } }; xmlhttp.open('POST', contentname + 'page.php', true); xmlhttp.send(); } if (document.getElementById('info').className == 'active') { document.getElementById('dropdown-icon').src = "/ressource/images/nydoo-dropdown-icon-black.png"; } else { document.getElementById('dropdown-icon').src = "/ressource/images/nydoo-dropdown-icon-yellow.png"; } } window.onload = change_content; </script> </head> <header> <ul id="mainnavigation"> <li id="mainlist" class="dropdown"><a id="info" class="notactive" onclick="change_content(this.id);">Information<img id="dropdown-icon" style="width: 15px; height: 12px; float: right; margin-top: 7px; margin-right: 20px;" src="/ressource/images/nydoo-dropdown-icon-yellow.png" id="dropdown_icon"></a></li> </ul> </header> <body onload="change_content('login'); return false;"> <div id="content"> </div> </body> </html> What I'm trying to do is changing the color from img (id="dropdown-icon") from yellow to black whenever a (id="info") has class "active". Also the other way around (class "notactive" -> change from black to yellow). Hopefully this describes it. It maybe a simple thing. But I'm getting frustrated with this issue. Let me know if I screwed it up myself :3 Thank you very much n advance.
×