Categories
JavaScript Answers

How to Wrap a Function with JavaScript?

Sometimes, we want to wrap a function with JavaScript.

In this article, we’ll look at how to wrap a function with JavaScript.

Wrap a Function with JavaScript

To wrap a function with JavaScript, we can return a function inside a function.

For instance, we write:

const wrap = (someFunction) => {
  const wrappedFunction = (...args) => {
    const newArgs = [...args, 'another argument']
    return someFunction(...newArgs)
  }
  return wrappedFunction
}

const doThing = (...args) => {
  console.log('do thing', args)
}


const wrappedDoThing = wrap(doThing)
wrappedDoThing('one', 'two', 'three')
doThing('one', 'two', 'three')

We have the wrap function that takes the someFunction function.

Then we define the wrappedFunction function inside the it that takes an unlimited amount of arguments.

We use the rest operator to get all the arguments and put them into the args array.

Next, we define the newArgs array with all the args spread into the array.

And we put 'another argument' into it as its last element.

Then we call someFunction with newArgs‘s element as the arguments by spreading it.

Finally, we return the wrappedFunction function.

Next, we have the doThing function that takes an unlimited number ofd arguments and we get them with the rest operator in an array and log them all.

Then we call wrap with doThing and assign it to wrappedDoThing.

Now we call wrappedDoThing and doThing with the same arguments.

And we should see:

"do thing", ["one", "two", "three", "another argument"]
"do thing", ["one", "two", "three"]

logged in the console.

We see that the first entry has an extra argument since we called wrappedFunction which is returned in wrap.

That added the extra argument.

Conclusion

To wrap a function with JavaScript, we can return a function inside a function.

Categories
JavaScript Answers

How to get the value of the currently selected Selectize.js input item?

Sometimes, we want to get the value of the currently selected Selectize.js input item.

In this article, we’ll look at how to get the value of the currently selected Selectize.js input item.

How to get the value of the currently selected Selectize.js input item?

To get the value of the currently selected Selectize.js input item, we can add a onChange handler into the Selectize option object.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.13.3/js/standalone/selectize.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.13.3/css/selectize.css" />

<select id='searchTextbox'>
  <option>apple</option>
  <option>orange</option>
  <option>grape</option>
</select>

to add the jQuery scripts, Selectize script and the Selectize CSS.

We also add the select element we use to convert to a Selectize drop down.

Then we write:

$('#searchTextbox').selectize({
  maxItems: 1, 
  maxOptions: 30, 
  onChange(value) {
    console.log(value);
  }
});

to select the select element with $.

And we call selectize on it with an object with the onChange method.

It takes the value parameter which is the selected item’s value.

Now when we select an item from the Selectize drop down, we should see the value logged in the console.

Conclusion

To get the value of the currently selected Selectize.js input item, we can add a onChange handler into the Selectize option object.

Categories
JavaScript Answers jQuery

How to Toggle Show or Hide on Click with jQuery?

Sometimes, we want to toggle show or hide on click with jQuery.

In this article, we’ll look at how to toggle show or hide on click with jQuery.

Toggle Show or Hide on Click with jQuery

To toggle show or hide on click with jQuery, we can use the toggle method in the click handler of the toggle button.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<button>
  toggle
</button>

<div>
  hello world
</div>

to add a toggle button and a div to toggle on and off.

Then we write:

$("button").click(() => {
  $('div').toggle("slide", {
    direction: "right"
  }, 1000);
});

to get the button with $ and add a click event handler to it with click.

In the click handler, we get the div with $.

And we call toggle on it with the effect to apply when toggling and an object to set the direction of the slide.

The 3rd argument is the number of milliseconds to show the transition effect when sliding.

Now when we click toggle, we should see ‘hello world’ being slid right and left when toggling off and on respectively.

Conclusion

To toggle show or hide on click with jQuery, we can use the toggle method in the click handler of the toggle button.

Categories
JavaScript Answers

How to Convert RGB Color Code to Hex with JavaScript?

Sometimes, we want to convert RGB color code to hex with JavaScript.

In this article, we’ll look at how to convert RGB color code to hex with JavaScript.

Convert RGB Color Code to Hex with JavaScript

To convert RGB color code to hex with JavaScript, we can get the RGB number values with the JavaScript string match method.

Then we can match each value to the corresponding hex number and join them together with the JavaScript string’s join method.

For instance, we write:

const RGBtoHEX = (color) => {
  return "#" + [...color.match(/\b(\d+)\b/g)]
    .map((digit) => {
      return parseInt(digit)
        .toString(16)
        .padStart(2, '0')
    })
    .join('');
};

console.log(RGBtoHEX('rgb(0, 70, 255)'))

We call color.match with /\b(\d+)\b/g to match the RGB color values from the color syring.

Then we use the spread operator to spread the returned results into an array.

Next, we call map with a callback to parse the numbers into integers, then we convert then to hex strings with toString and 16 as its argument.

Then we call padStart to pad the returned string with a zero to make its length 2.

Finally, we call join to join the mapped strings together.

Therefore, the console log should show "#0046ff".

Conclusion

To convert RGB color code to hex with JavaScript, we can get the RGB number values with the JavaScript string match method.

Then we can match each value to the corresponding hex number and join them together with the JavaScript string’s join method.

Categories
JavaScript Answers

How to Check if Any Div Contain a Word with JavaScript?

Sometimes, we want to check if any div contains a word with JavaScript.

In this article, we’ll look at how to check if a any div contains a word with JavaScript.

Check if Any div Contains a Word with JavaScript

To check if a div contains a word with JavaScript, we can select all the divs and then loop through them with the for-of loop.

Then we can get the text content of each div in the loop body using the textContent property.

And then we can check if the div includes the text we’re looking for with the JavaScript string’s includes method.

For instance, if we have:

<div class='titanic'>
  foo
</div>
<div class='titanic'>
  bar
</div>
<div class='titanic'>
  baz
</div>

Then we can check which div has the word ‘bar’ in it by writing:

const divs = document.querySelectorAll('div')
for (const d of divs) {
  if (d.textContent.includes('bar')) {
    console.log('has bar')
  }
}

We select all the divs and assign it to divs with:

const divs = document.querySelectorAll('div')

Then we loop through the node list object we assigned to divs by writing:

for (const d of divs) {
  if (d.textContent.includes('bar')) {
    console.log('has bar')
  }
}

We get the text content of each div with d.textContent.

Then we call includes with 'bar' to check if the text content of the div includes 'bar'.

If that’s true, we log 'has bar'.

Therefore, we should see 'has bar' logged once in the console since ‘bar’ appears in one div.

Conclusion

To check if a div contains a word with JavaScript, we can select all the divs and then loop through them with the for-of loop.

Then we can get the text content of each div in the loop body using the textContent property.

And then we can check if the div includes the text we’re looking for with the JavaScript string’s includes method.