William Tio Wee Leong
William Tio Wee Leong

Reputation: 473

Passing a variable declared in a view to javascript

I stuck in this part.

I have declared a integer variable called "count"

@{
    int count = 6;
}

I have a slider as well.

<input name="sliderStatus"  id="sliderStatus" onchange="updateThumbnail(this, @count)" type="range" min="0" max="100" value="0" step="1"/>

I have script something like this...

    <script type="text/javascript">
    function updateThumbnail(rangeInput, count) {
        var previousImage = document.getElementById('someImage' + rangeInput / (rangeInput.value / count));
        if (previousImage) {
            previousImage.style.display = 'none';
        }
        var nextImage = document.getElementById('someImage' + rangeInput / (rangeInput.value / count));
        nextImage.style.display = 'block';
    }
</script>

The script turns out that it cannot recognize the "count" in the script. How do it pass the variable from the view into the script? Help appreciated. :(

Upvotes: 0

Views: 1783

Answers (1)

John x
John x

Reputation: 4031

try

@{
    int count = 6;
}

<script type="text/javascript">

var $count = '@count';

</sxript>

Upvotes: 3

Related Questions