/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Having Trouble Making A Coupon Code Function

I’m trying to create a simple coupon code area for my website. I thought I’d be able to figure it out, but I’m having some trouble. Essentially, I need a code that will take a text input and pump it out as a correlating numerical output. For example: bigdiscount = -50. I’m getting the output with my current code, but the problem is “-50” shows up no matter what I type in as input. The “else” statement seems to have no effect. I feel like I must be close, but I’m missing something…or maybe I’m trying to do this too simply. Any ideas? Thanks in advance!

Here’s my code right now:
(In head?

function searchForModifier(){
var modiferCode = document.getElementById(“modifierCodeText”).value;
var discount

if (modifierCode = “bigdiscount”) {
discount = -50;
}
else{
discount = 0;
}
document.getElementById(“priceModify”).value = discount;
return;
}

(In body?

<input type=”text” id=”modifierCodeText” size=”15″ onkeydown=”document.getElementById(‘priceModify’).value=””>
<br />
<input type=”button” onclick=”searchForModifier()” value=”Click here to find your price modifier”>
Price Modifier:
$<input name=”pricemodifier” type=”text” id=”priceModify” size=”5″ readonly>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@FangJun 07.2008 — Compareif (modifierCode [B][COLOR="Blue"]==[/COLOR][/B] "bigdiscount") {
Copy linkTweet thisAlerts:
@naturalelementsauthorJun 07.2008 — I tried using == instead of =, but when I use ==, it runs into an error no matter what the input is. I'm not really sure why that doesn't work, but I'm still stuck in the same place. My code right now looks like this:

[CODE]function searchForModifier(){
var modiferCode = document.getElementById("modifierCodeText").value;
var discount

if (modifierCode = "bigdiscount") {
discount = -50
}
else if (modifierCode = "yo") {
discount = -5
}
else {
discount = 0
}
document.getElementById("priceModify").value = discount;
return;
}[/CODE]
Copy linkTweet thisAlerts:
@FangJun 07.2008 — function searchForModifier(){
var modiferCode = document.getElementById("modifierCodeText").value;
var discount

if (modifierCode [B][COLOR="Blue"]==[/COLOR][/B] "bigdiscount") {
discount = -50
}
else if (modifierCode [B][COLOR="Blue"]==[/COLOR][/B] "yo") {
discount = -5
}
else {
discount = 0
}
document.getElementById("priceModify").value = discount;
return;
}
Copy linkTweet thisAlerts:
@naturalelementsauthorJun 07.2008 — Okay, I tried that, but with the double equal sign, the code runs into an error every time and does not return any value at all. However, with the single equal sign, it always returns "-50", no matter what the input is. Something else must be wrong still...
Copy linkTweet thisAlerts:
@FangJun 07.2008 — function searchForModifier(){
var modiferCode = document.getElementById("modifierCodeText").value;
var discount;
switch (modiferCode) {
case "bigdiscount":
discount = -50;
break;
case "yo":
discount = -5;
break;
default:
discount = 0;
}
document.getElementById("priceModify").value = discount;
}
Copy linkTweet thisAlerts:
@naturalelementsauthorJun 07.2008 — It works! Thanks a lot Fang - I really appreciate it!
×

Success!

Help @naturalelements spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...