The problem breaks easily when the body of addProp changes to this:
function addProp(id) { var el = document.getElementById(id); if (el) { myProps.push(id + "=" + encodeURIComponent(el.value)); } else { alert('Not found: ' + id); } }
boy_girl and email not in this HTML:
Boy: <input type="radio" name="boy_girl" id="boy_girl_b" value="boy"/> Girl:<input type="radio" name="boy_girl" id="boy_girl_g" value="girl"/></li> ... <input type="text" name="email" id="check" maxlength="64" class="required" />
You can fix it something like this:
function addProp(name) { var els = document.getElementsByName(name); if (els.length) { myProps.push(name + "=" + encodeURIComponent(els[0].value)); } else { alert('Not found: ' + name); } }
But in fact, this is only the beginning of the story. myProps are local to the insert function, but referenced by the postData function; you show validation error signs for all fields no matter what fields were actually filled ... Also, your code is a bit WET - for example, all of these
var log = document.getElementById("log"); log.className = 'show'; var log1 = document.getElementById("log1"); log.className = 'show'; ...
... easily transforms into this:
var showValidationError = function(id) { var el = document.getElementById(id); if (el) { el.className = 'show'; } else { alert('Missing element #' + id); } } ... showValidationError('log'); for (var i = 1; i < 10; i++) { showValidationError('log' + i); }
I understand that you are completely fresh with JS; but itβs not about freshness, but about the organization of your code.
source share