user2171512
user2171512

Reputation: 561

Set default parameter in form

@{  List<System.Web.Mvc.SelectListItem> Month = new List<System.Web.Mvc.SelectListItem>();

}
<script type="text/javascript">


    function setImage(param) {
        document.getElementById('ReportParameter').value = param;
//        alert(document.getElementById('ReportParameter').value);
    }

</script>

<form id="reportParametersForm" method="GET" action="@string.Format("{0}/{1}", @Url.Content("~/Reports/View"), ViewBag.Report)">
<fieldset style="padding: 0.2em 0 1.2em 0; height: 50px">
    <legend style="margin: 0 1px 0 10px; padding: 3px 36px 3px 20px; background-color: #494949;
        color: white; font-size: 11pt;">
        @Html.Resource(String.Format("Report_{0}", ViewBag.Report as string))</legend>
    <table border="0" cellspacing="0" cellpadding="0" style="font-size: x-small">
       <input type="hidden" name="ReportParameter" id="ReportParameter" value=""/>
    </table>
    <div align="center">   
        <button class="button" id="Day" style="width:90px; height:46px" type="submit" onclick="setImage('1') ;">
            Day</button>

         <button class="button" id="Week" style="width:90px; height:46px" type="submit" onclick="setImage('2') ;">
            Week</button>

         <button class="button" id="Month" style="width:90px; height:46px" type="submit" onclick="setImage('3') ;">
            Month</button>           

    </div>
    <input type="hidden" name="ShowToolBar" value="false" />

</fieldset>
</form>
<script type="text/javascript">
    var links = $('button');

    links.click(function () {
        links.css('background-color', '#2B2B2B');
        $(this).css('background-color', '#BE4856');
    });
</script>

How i can set image 1 to be selected as default, when i open the page where i have 3 different buttons and when some button i selected i see different image? I want to see image 1 on my screen and button 1 to be selected as default, but when i select button 1,2,3 to be able to see other images also

Upvotes: 1

Views: 710

Answers (2)

Sergii Shvager
Sergii Shvager

Reputation: 1256

Create function setDefaults(), and call it on start To make more clear code, you can pass data attribute for related image in the html to each button.

         <button class="button" id="Day" style="width:90px; height:46px" type="submit" data-image="1">
            Day</button>

         <button class="button" id="Week" style="width:90px; height:46px" type="submit" data-image="2">
            Week</button>

         <button class="button" id="Month" style="width:90px; height:46px" type="submit" data-image="3">
            Month</button>  

And helper function setActiveButton() which changes styles.

function setDefaults(buttonId) {
  var button = $('#' + buttonId);
  setActiveButton(buttonId);
  setImage(button.data('image'));
}

function setActiveButton(buttonId) {
  var links = $('button');
  links.css('background-color', '#2B2B2B');
  $('#' + buttonId).css('background-color', '#BE4856');
}

And inside your script tag

<script type="text/javascript">
    var links = $('button');

    links.click(function () {
        setActiveButton(this.id);

    });
    setDefaults('Day');
</script>

Upvotes: 1

hudsond7
hudsond7

Reputation: 706

You can run the function when the script is loaded with a default value.

    function setImage (param) {
        document.getElementById('ReportParameter').value = param;
    }

    setImage(1);

Upvotes: 0

Related Questions