I want to add some value in an input field with jQuery in my Smarty tpl file. If the hostname input field is left blank and you hit the order button it should add an general domain name to the input field. Like w1.test.local. But its not working
Smarty tpl file:
<div>
<input class="hostname-box" type="text" name="domain" required="" value="">
<a href="#host" class="button">Add</a>
</div>
<div>
<a href="#submit" class="button">Order</a>
</div>
JQuery
$('.button').click(function(){
var fieldID = $(this).prev().attr("hostname-box");
fieldID = fieldID.replace(/([\[\]]+)/g, "\\$1");
$('#' + fieldID).val("hello.domain.local");
});
But getting this error :
VM3715:3 Uncaught TypeError: Cannot read property 'replace' of undefined at HTMLAnchorElement. (:3:23) at HTMLAnchorElement.dispatch (jquery.js:3) at HTMLAnchorElement.r.handle (jquery.js:3)
You can use the input class as a selector to change it's value :
$('.button').click(function(){
var jHostName = $('.hostname-box');
var hostNameValue = jHostName.text().trim();
if(hostNameValue === '') {
jHostName.val("hello.domain.local");
}
});
I personally would use other class than button
, since it's too generic.