Jump to content

Custom script not working


Adam

Recommended Posts

Hi

I have a script that will ultimately be used to extend the functionality of one of the modules.

It is used to count the values of all inputs and then, if too large a number is entered, correct the value on the front.

Here's what it looks like.

<input min="0" name="cat1" step="1" type="number" max="50" value="0" />

<input min="0" name="cat2" step="1" type="number" max="50" value="0" />

<input min="0" name="cat3" step="1" type="number" max="50" value="0" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
<script type="text/javascript">
        var max = 50;
var $inputs = $('input');


function sumInputs($inputs) {
  var sum = 0;
 
  $inputs.each(function() {
    sum += parseInt($(this).val(), 0);
  });

  return sum;
}


$inputs.on('input', function(e) {
  var $this = $(this);
  var sum = sumInputs($inputs.not(function(i, el) {
    return el === e.target;
  }));
  var value = parseInt($this.val(), 10) || 0;
  if(sum + value > max) $this.val(max - sum);
});

</script>


It works fine in html, but not in the store.

To make things easier, I added the code to one of the tpl files to make it easier - removing the original content first.

The browser shows it as in HTML - the function refers to the inputs, but in the front-end store it does not correct the values (screens).


Request for help.
Thanks in advance.

obraz.png

obraz.png

Edited by Adam (see edit history)
Link to comment
Share on other sites

Hi

Thanks, this is how it will be done in the end. Now I'm trying everything in the tpl file because it didn't work in *.js either :)

Interesting fact, it is not known yet what it is.

Link to comment
Share on other sites

module hook:

public function hookHeader()
{
  if ($this->context->controller->php_self == 'product') {  // identify page name  
    $this->context->controller->addJS($this->_path.'views/css/script.js'); // your module javascript
  }
}

Sometimes it's better to show the page so a person can check it.
It is not always possible to solve everything according to the picture.
I'll give you a picture of the car and leave the front hood closed and ask you to tell me where my engine oil is leaking 😄

 

Edited by ps8modules.com (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...