php_guest Posted June 6, 2009 Share Posted June 6, 2009 I have no idea why the function is not working if I put on the 4th line Example instead of 'test'. So if I put if(document.getElementById('test').value=='') is working, if I put if(document.getElementById(Example).value=='') is not working. It must be some dummy mistake I can not find it! Please help me. function validate(Example) { var isGood = true; //validate test if(document.getElementById('test').value=='') { isGood = false; } //return true or false if (isGood== false) { alert (isGood); } return isGood; } $(function() { $('#addquestion').click(function() { $('<input id="test" type="text" name="text1" /><br />').appendTo('#to_moreoptions'); if(document.getElementById('formid').value=='vrednost') { validate('test'); } }); }); Link to comment https://forums.phpfreaks.com/topic/161206-a-problem-with-a-simple-function/ Share on other sites More sharing options...
Ken2k7 Posted June 6, 2009 Share Posted June 6, 2009 function validate (id) { return document.getElementById(id).value !== ''; } Link to comment https://forums.phpfreaks.com/topic/161206-a-problem-with-a-simple-function/#findComment-850629 Share on other sites More sharing options...
php_guest Posted June 6, 2009 Author Share Posted June 6, 2009 sorry I don't understand what you mean by this. I don't understand why test is not passed via Example. Link to comment https://forums.phpfreaks.com/topic/161206-a-problem-with-a-simple-function/#findComment-850659 Share on other sites More sharing options...
php_guest Posted June 6, 2009 Author Share Posted June 6, 2009 I will make more simple question. The problem is that 'test' is not passed. The following function alert undefined, but it should alert test. function validate(Example) { var isGood = true; alert(Example); } $(function() { //if I put validate('test') here, it will alert test if(document.getElementById('formid').value=='vrednost') { validate('test'); } }); Link to comment https://forums.phpfreaks.com/topic/161206-a-problem-with-a-simple-function/#findComment-850720 Share on other sites More sharing options...
Ken2k7 Posted June 7, 2009 Share Posted June 7, 2009 Would this alert the correct data? function validate (id) { window.alert(id); } if (document.getElementById("formid").value === "vrednost") window.validate("test"); Link to comment https://forums.phpfreaks.com/topic/161206-a-problem-with-a-simple-function/#findComment-850751 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.