Skip to content

Commit 84afee9

Browse files
committed
feat: custom subject message
1 parent 0fd491e commit 84afee9

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

js/contact.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,31 @@ const EMAILJS_TEMPLATE_ID = 'template_zwwhu2y';
55

66
emailjs.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+
824
async 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.';

pages/contact.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ <h3>📬 drop us a note</h3>
6363
<option>Something Else</option>
6464
</select>
6565
</div>
66+
<div class="form-group" id="custom-subject-group" style="display:none">
67+
<label for="cf-custom-subject">custom subject</label>
68+
<input id="cf-custom-subject" name="custom_subject" type="text" placeholder="e.g. lost cat near campus">
69+
</div>
6670
<div class="form-group">
6771
<label for="cf-message">your message</label>
6872
<textarea id="cf-message" name="message" placeholder="Kaddu swallowed a paper again!" required></textarea>

0 commit comments

Comments
 (0)