jasonc Posted September 15, 2011 Share Posted September 15, 2011 is there a way to do this with javascript ? var fID = document.getElementById('email2'); Link to comment https://forums.phpfreaks.com/topic/247188-how-do-i-check-if-documentgetelementbyidemail2-exists-before-getting-value/ Share on other sites More sharing options...
codefossa Posted September 15, 2011 Share Posted September 15, 2011 if (window.document.getElementById("email2").value != undefined) { // Do This } Link to comment https://forums.phpfreaks.com/topic/247188-how-do-i-check-if-documentgetelementbyidemail2-exists-before-getting-value/#findComment-1269530 Share on other sites More sharing options...
web_craftsman Posted September 15, 2011 Share Posted September 15, 2011 var fID = document.getElementById('email2'); if (fID) { // do something with it's value alert(fID.value); // ... } Link to comment https://forums.phpfreaks.com/topic/247188-how-do-i-check-if-documentgetelementbyidemail2-exists-before-getting-value/#findComment-1269533 Share on other sites More sharing options...
jasonc Posted September 15, 2011 Author Share Posted September 15, 2011 if (window.document.getElementById("email2").value != undefined) { // Do This } hey thanks for the quick reply. i tried that but got the following error message Error: window.document.getElementById("email2") is null sometimes the email2 field is not present on the form, it would only be present if the user was not logged in already to confirm their email was correctly entered. if they are logged in then there is only one field (email1) for the email not two (the other being email2) Link to comment https://forums.phpfreaks.com/topic/247188-how-do-i-check-if-documentgetelementbyidemail2-exists-before-getting-value/#findComment-1269538 Share on other sites More sharing options...
jasonc Posted September 15, 2011 Author Share Posted September 15, 2011 ah i think i got it working, but please confirm that this is correct before i use this. if (document.getElementById("email2") != undefined) { // Do This } Link to comment https://forums.phpfreaks.com/topic/247188-how-do-i-check-if-documentgetelementbyidemail2-exists-before-getting-value/#findComment-1269543 Share on other sites More sharing options...
codefossa Posted September 15, 2011 Share Posted September 15, 2011 Yeah, sorry didn't realize I did that. You should be good to go. Link to comment https://forums.phpfreaks.com/topic/247188-how-do-i-check-if-documentgetelementbyidemail2-exists-before-getting-value/#findComment-1269544 Share on other sites More sharing options...
jasonc Posted September 15, 2011 Author Share Posted September 15, 2011 great thank you for your help on this Link to comment https://forums.phpfreaks.com/topic/247188-how-do-i-check-if-documentgetelementbyidemail2-exists-before-getting-value/#findComment-1269548 Share on other sites More sharing options...
web_craftsman Posted September 16, 2011 Share Posted September 16, 2011 This [js] if (document.getElementById("email2") != undefined) { // Do This } [/js] is a more clumsy code for [js] if (document.getElementById("email2")) { // Do This } [/js] Because javascript will convert undefined to logical type false, and not-null object(if element exists) - at true Link to comment https://forums.phpfreaks.com/topic/247188-how-do-i-check-if-documentgetelementbyidemail2-exists-before-getting-value/#findComment-1269798 Share on other sites More sharing options...
Adam Posted September 16, 2011 Share Posted September 16, 2011 You can also cut out the inevitable second call to getElementById in that situation with: var el; if (el = document.getElementById('...')) { // ... } Link to comment https://forums.phpfreaks.com/topic/247188-how-do-i-check-if-documentgetelementbyidemail2-exists-before-getting-value/#findComment-1269816 Share on other sites More sharing options...
jasonc Posted September 16, 2011 Author Share Posted September 16, 2011 Thank you for all your replies, I have opted for Kira method. Link to comment https://forums.phpfreaks.com/topic/247188-how-do-i-check-if-documentgetelementbyidemail2-exists-before-getting-value/#findComment-1269822 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.