2

Guys I'm new to JavaScript, so not sure where the error is. Basically I've two submit buttons in my cshtml. one with the id = email and other id = sms. I've set value=" ". I'm using these buttons in a form.

what I want to do is; by clicking on these button I want to pass them a value so that I can use that value in model and controller, like in switch statement. tried various ways but still its passing null value. please advise!

function GetVal() {
    var input = document.getElementsByTagName('input');
    for (i = 0; i < input.length; i++) {
        if (input[i].type == 'submit') {
            if (input[i].id == 'email') {
                //input[i].value = 'submitEmail';
                //input[i].setAttribute("value", "subEmail");
                document.getElementById("email").setAttribute("subEmail", "value");
                break;
            }
        }
    }
}

<input id="email" type="submit" class="@( Model.UnsubscribedEmail == null ? "unsubscribe" : "subscribe")" onclick="GetVal();" />

Model

public static void susbscription(MyModel model, string submitButton)
    {
     switch (submitButton)
        {
            case "subEmail":
            //Code here
            break;
         }
     }

Controller

[HttpPost]
public ActionResult MarketingPreferences(MyModel model, string submitButton)
    {
        MarketingPreferencesModel.susbscription(model, submitButton);

        return View(model);
    }
2
  • input[i].value = 'submitEmail' should work, are you sure its finding the right element? Commented Aug 23, 2012 at 15:25
  • yeah. i've used alert to display the element and it was right Commented Aug 23, 2012 at 15:27

1 Answer 1

1

Try this:

<input id="email" type="submit" name="submitButton" onclick="GetVal(this);" class="@( Model.UnsubscribedEmail == null ? "unsubscribe" : "subscribe")"  />

In JavaScript:

function GetVal(element) {
    element.value = "subEmail";
}    

In Action:

public ActionResult MarketingPreferences(MyModel model, string submitButton)
{
    MarketingPreferencesModel.susbscription(model, submitButton);

    return View(model);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.