Search code examples
javascriptphpjquerytemplatessmarty

Dynamic hostname in Smarty input field with jQuery on click event


At the moment I am trying to create an dynamic hostname with an jQuery on click event. I have this so far

Smarty:

<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 jHostName = $('.hostname-box');
var hostNameValue = jHostName.text().trim();

var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

for( var i=0; i < 5; i++ )
   text += possible.charAt(Math.floor(Math.random() * possible.length));

if(hostNameValue === '') {
     jHostName.val(text+".hostname.local");
}
});

I can generate an text on click like random.hostname.local. But the goal is to achieve to generate an text like vps{random}-{currentDate}.hostname.local.


Solution

  • Like this?

        var hostNameValue = '';
        var text = "vps-";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    
        for( var i=0; i < 5; i++ ) {text += possible.charAt(Math.floor(Math.random() * possible.length));}
    
        let date = new Date();
        text += '-'+date.toISOString().substr(0,10);
      
        if(hostNameValue === '') {
          console.log(text+'.hostname.local');
        }