@@ -5,13 +5,31 @@ const EMAILJS_TEMPLATE_ID = 'template_zwwhu2y';
55
66emailjs . init ( EMAILJS_PUBLIC_KEY ) ;
77
8+ document . addEventListener ( 'change' , ( e ) => {
9+ if ( e . target ?. id !== 'cf-topic' ) return ;
10+ const customSubjectGrp = document . getElementById ( 'custom-subject-group' ) ;
11+ const customSubjectInput = document . getElementById ( 'cf-custom-subject' ) ;
12+ const isSomethingElse = e . target . value === 'Something Else' ;
13+ if ( customSubjectGrp ) customSubjectGrp . style . display = isSomethingElse ? 'flex' : 'none' ;
14+ if ( customSubjectInput ) {
15+ if ( isSomethingElse ) {
16+ customSubjectInput . setAttribute ( 'required' , '' ) ;
17+ } else {
18+ customSubjectInput . removeAttribute ( 'required' ) ;
19+ customSubjectInput . value = '' ;
20+ }
21+ }
22+ } ) ;
23+
824async function submitForm ( event ) {
925 event . preventDefault ( ) ;
1026
11- const name = document . getElementById ( 'cf-name' ) ?. value . trim ( ) ;
12- const email = document . getElementById ( 'cf-email' ) ?. value . trim ( ) ;
13- const topic = document . getElementById ( 'cf-topic' ) ?. value . trim ( ) ;
14- const message = document . getElementById ( 'cf-message' ) ?. value . trim ( ) ;
27+ const name = document . getElementById ( 'cf-name' ) ?. value . trim ( ) ;
28+ const email = document . getElementById ( 'cf-email' ) ?. value . trim ( ) ;
29+ const rawTopic = document . getElementById ( 'cf-topic' ) ?. value . trim ( ) ;
30+ const customSubject = document . getElementById ( 'cf-custom-subject' ) ?. value . trim ( ) ;
31+ const topic = rawTopic === 'Something Else' ? ( customSubject || '' ) : rawTopic ;
32+ const message = document . getElementById ( 'cf-message' ) ?. value . trim ( ) ;
1533
1634 const successEl = document . getElementById ( 'formSuccess' ) ;
1735 const errorEl = document . getElementById ( 'formError' ) ;
@@ -20,10 +38,14 @@ async function submitForm(event) {
2038 if ( errorEl ) errorEl . textContent = '' ;
2139 if ( successEl ) successEl . style . display = 'none' ;
2240
23- if ( ! name || ! email || ! topic || ! message ) {
41+ if ( ! name || ! email || ! rawTopic || ! message ) {
2442 if ( errorEl ) errorEl . textContent = 'Please fill in all fields.' ;
2543 return ;
2644 }
45+ if ( rawTopic === 'Something Else' && ! customSubject ) {
46+ if ( errorEl ) errorEl . textContent = 'Please enter a custom subject.' ;
47+ return ;
48+ }
2749
2850 const originalBtnText = submitBtn ?. textContent ?? 'send it → 🐾' ;
2951 if ( submitBtn ) { submitBtn . disabled = true ; submitBtn . textContent = 'Sending…' ; }
@@ -40,6 +62,10 @@ async function submitForm(event) {
4062 setTimeout ( ( ) => { successEl . style . display = 'none' ; } , 5000 ) ;
4163 }
4264 document . getElementById ( 'contact-form' ) ?. reset ( ) ;
65+ const customSubjectGrp = document . getElementById ( 'custom-subject-group' ) ;
66+ const customSubjectInput = document . getElementById ( 'cf-custom-subject' ) ;
67+ if ( customSubjectGrp ) customSubjectGrp . style . display = 'none' ;
68+ if ( customSubjectInput ) customSubjectInput . removeAttribute ( 'required' ) ;
4369 } catch ( err ) {
4470 console . error ( 'EmailJS error:' , err ) ;
4571 if ( errorEl ) errorEl . textContent = 'Something went wrong. Please try again or email us directly.' ;
0 commit comments