I want to highlight a list of input received in an array and give the focus to the first of them.
<head>
<script type='text/javascript' src='app.js'></script>
</head>
<body>
...
<script>showErrors(['firstName', 'lastName'])</script>
</body>
in app.js, I put the following javascript code:
function showErrors($fields)
{
$fields.forEach(function($field){
console.log($field);
$('#'+$field).addClass('error-custom');
$('#'+$field).css('border', 'red solid 2px');
});
//set focus to first field
if($fields.length > 0)
{
$('#'+$fields[0]).focus();
}
}
The JS code is executed but before the page is rendered.
I tried to put the function showErrors into $(document).ready(function(){...} but it still had no effect.
Where should I place the code so I can have apply after the page is rendered?
$(document).ready(function(){ ...your code here...})should be the right place to do iterror-customclass.showError($fields)inside the block$(document).ready(function(){showError($fields){...rest of code ..}});when I open the page in Chrome, it shows the following error in the development tool: "Uncaught ReferenceError: showErrors is not defined" when I view the source Ctrl+U in chrome, the app.js is correct and when I click on its link, it shows my code.<script type='text/javascript' src='http://localhost/crm/public/js/jquery.min.js'></script>