I am using the input type of range - because aesthetically it looks nice on my site.
However, I want to convert its number value to change the alignment of my output text. I am doing a font foundry style website. This website uses symbols that you can select to change the font alignment but I want to stick to the range slider and I am getting stuck about how to think about it. I am still learning javascript.
Can anyone help me think about this in vanilla javascript?
Range type is marked up like this -
input type=“range” name=“spacingSizeOne” min=1 max=3 value=2 step=1></input
Javascript - I want to do something like this and convert the number to a string essentially…
//when a range position selected update font alignment
//if input is 1 = left align
//if input is 2= center align
//if input is 1= right align
//default - is 2 = center
Wondering if it is possible and if so along these lines - although I know this isn’t right.
if (this = 1 )
outputOneTag.style.textAlign = “left”
@V_O - if I’m understanding you correctly this should be fairly straightforward with JavaScript and the oninput event listener.
Basically all you need to do is set up the values on your slider, then set up a listener to change the text alignment when the user changes the value of the range input.
So something like this should work:
rangeElement.addEventListener('input', function () {
let value = rangeElement.value;
switch(value) {
case 1:
targetElement.style.textAlign = 'left';
break;
case 2:
targetElement.style.textAlign = 'center';
break;
case 3:
targetElement.style.textAlign = 'right';
break;
}
}, false);
Since I don’t have your code I haven’t tested this but it should be pretty easy to pull of with something like this.
Hi, Thank you so much for replying. I have put the code in and I thought I had wired it up properly but it isn’t working. This is my read only link - would you be able to take a look and see where I am going wrong?