I have an HTML form in the view and I need to fill out some input fields with json variables that come from the server. I don't want to write javascript code for each input field, instead, I want to access json variable in the value
attribute of input tag.
I tested onload
event for input
tag but ot does not have any effects.
<input type="text" class="form-control" onload="this.value = 'value';" name="company[name]" />
onload
event is work with <body>
tag and some other tags, or you can also use window.onload = function(){}
see below sample code
HTML
<input type="text" class="form-control" name="company[name]" id="company_name"/>
JS CODE
window.onload = function(){
document.getElementById("company_name").value = "value";
}
PS: id
will be unique selector so add id attribute in input element.
you can access all element using right selector in window.onload
event anonymous function
UPDATE
suggested by @AlexanderO'Mara
'onload' is supported by the following HTML tags:
<body>, <frame>, <frameset>, <iframe>, <img>, <link>, <script>
And the following Javascript objects:
image, layer, window