I would like to pass a parameter to the jQuery document.ready() function from my View:
$(document).ready(function (parameter){
$('select[name=Product]').val(parameter);
});
How can I fire the event from my View and pass the parameter? I use Razor as View engine.
Thanks
jQuery ready() Method The ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs.
The key difference between $(document). ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc.
The ready() method is used to make a function available after the document is loaded. Whatever code you write inside the $(document ). ready() method will run once the page DOM is ready to execute JavaScript code.
We can have multiple document. ready() function in our code but only one body. onload() is allowed.
You can't. The document.ready function doesn't take parameters. You could for example define this parameter as a global variable in your view:
<script type="text/javascript">
var model = @Html.Raw(Json.Encode(Model));
</script>
and then in your separate javascript file use this global variable:
$(function() {
$('select[name=Product]').val(model.SomeProperty);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With