JavaScript - jQuery - Ajax


  1. » JanMolendijk
    Plessant coursesweb,

    I have a javascript what turns color-codes into color-names.
    The problem is I have 20 results from color-codes, and I can...
    Last post » MarPlo
    Try use the following code:

    <script>
    const clr_codes = ;

    var clr_rgb_name =[];

    for(var i=0; i<clr_codes.length; i++){...


  2. » Marius
    I'm trying to create a script for a web page that has an audio like this:

    <div class='class1' id='id1'>
    <audio src='link_to_audio'...
    Last post » MarPlo
    Since the audio tag is a child of #id1 you can use document.querySelector() .

    To set the volume to 50%, simply use audioElem.volume = 0.5;...


  3. » Marius
    I am trying to parse a JSON string 'Hello test' containing double quote which already escaped.

    JSON.parse('{ x : Hello \ test }')

    But I get...
    Last post » MarPlo
    You just need to escape the backslash \, so it turns into two backslashes \\

    let obj = JSON.parse('{ x : Hello \\ test }')
    console.log(obj)


  4. » Marius
    I have converted a database table into a JSON object.
    This is in essence a two-dimensional array, with each record being the first dimension, which...
    Last post » MarPlo
    You can use Object.keys to get the list of keys in the Object as an array and then get the length from it:

    Object.keys(act_obj_array).length;


  5. » Marius
    Quick question:
    How can I set a button that can increment and/or decrement at random a number as it is clicked?
    This is the code i have....
    Last post » MarPlo
    You can use Math.random() to decide whether you are going to increment or not:

    <button id='tst_btn'>Change counter</button>
    <h3...


  6. » Marius
    I'm trying to convert 8-bit integer to Hex color value. (e.g. FFFFFF)
    The 8-bit color integer is generated with the following formula:

    color =...
    Last post » MarPlo
    If you have an integer you can do with:

    color.toString(16)

    And it will turn it in to a hex string.

    // White
    color = (255 * 65536) + (255 *...


  7. » Marius
    In my application, I have a lot of utility functions that do little things from parsing strings to making toasts and so on.
    My question is how do I...
    Last post » MarPlo
    You can use Mixins.
    1. Import the component which you need.
    2. add mixin array as below in your component just above the data section (or...


  8. » Marius
    I have the following problem to solve it in JavaScript:
    - Find the text which is between small brackets and shift that text with the brackets to...
    Last post » MarPlo
    Try the following code:

    function testToEnd(str){
    //get matched string
    let st = str.match(/ *\( +\) */g)
    if(st){
    st = st ;

    // replace the...


  9. » Marius
    I am creating a website with HTML and JavaScript that relies on the data of an XML file hosted on a separate domain.
    I can achieve this with...
    Last post » MarPlo
    Try using the the fetch api.

    fetch('//example.com/file_address')
    .then( response => response.text() )
    .then( response => {
    //response is...


  10. » Marius
    I have a for() loop like this one which shows what i want to do.

    for(var i=0; i<15; i++) {
    // Add data in html
    // When data has a specified...
    Last post » MarPlo
    To achieve your goal you can add the if() condition within the loop, and check the values of your data.
    If it has the specified value, sets the new...


  11. » Marius
    With the good old for loop in Javascript I can do things like:

    for (let i=0; i<bla.length; i+=2){
    //...
    }

    So for every count, I can skip...
    Last post » MarPlo
    You may add an if() statement within the for...of loop, like in the following example (the original index is preserved).

    for (const of...


  12. » Marius
    How can I make that when an option from dropdowna <select> list is selected, the value of that option be displayed into a Div.
    I have this...
    Last post » MarPlo
    You have to start with a <select> element which raises a 'change' event when an option is selected.
    Inside that event, 'this.value' refers to...


  13. » Marius
    Say I have the following array of persons:

    const arr =

    And I want to find a certain person (name) in this array, and put it as the first...
    Last post » Admin
    You could sort the array with the sort() method.
    This approach moves all objects with the wanted 'name' to top.

    const arr = ;
    let first =...


  14. » Marius
    I have a nested object in JavaScript that looks like this:

    const yo = {
    one: {
    value: 0,
    mission: 17},
    two: {
    value: 18,
    mission: 3},...
    Last post » Admin
    Try combine the Object.values() and map() methods, like in the following example:

    const yo = {
    one: {
    value: 9,
    mission: 17
    },
    two: {...


  15. » Marius
    I’m trying to replace all spaces within a string with hyphens.
    I tried this:

    let str ='This is my text';
    str = str.replace(/\s/, '-');...
    Last post » Admin
    Add the global search flag (/g ) to your regex to match all occurrences.

    let str ='This is my text';
    str = str.replace(/\s/g, '-');...


  16. » Marius
    The code below sends the 30s event do Google Analytics after 30 seconds a user enters a page.

    setTimeout(function(){
    gtag('event', '30s');
    },...
    Last post » Admin
    You can watch for when a tab loses focus by using the event listener 'visibilitychange'. When the visibility changes, you can use 'document.hidden'...


  17. » Marius
    I have a JavaScript code that adds an input field for a user:

    var user = O'Conner, John ;
    b.innerHTML += <input type='hidden' value=' + user +...
    Last post » Admin
    You can replace the character with its HTML entity.

    As for ' - It can either be &rsquo; or &lsquo;

    var user = O'Conner, John ;
    user =...


  18. » Marius
    The idea is that when i click on a button, old content gets replaced by new HTML content added with javascript.
    I want that new content to appear...
    Last post » Admin
    To trigger a CSS transition, change the CSS state after you inserted the HTML. You can do this by changing a class (on the container or an inserted...


  19. » Marius
    I want to create a toggle function for my localStorage where I want it to remember the user's choice.
    I want it load the current preference on page...
    Last post » Admin
    You can't keep boolean in localStorage. That's why you have an error. Try to keep, for example, 0 for False and 1 for True. But remember that...


  20. » Marius
    I've a website loader in GIF format but it doesn't appear because the page is loaded in 0.01 seconds.
    Is there possible to set a loading time to 2...
    Last post » Admin
    Here is an example of a preloader with jQuery using the delay() method (2 sec = 2000), wrapped inside the 'load' event.

    <style>
    .loading...


  21. » Marius
    What's an efficient way of implementing the dotProduct method (to get the Dot product of two arrays) without importing any new Javascript libraries?...
    Last post » Admin
    Here is a method.
    Use the map() function to create a new array with multiplied results of each index and the reduce() function to sum the values of...


  22. » Marius
    I'm trying to push an array into the same array in javascript, But it doesn't seem to be working; the third element is added continuously.
    Here is...
    Last post » Admin
    You are trying to push the same reference to the array. So, when the array updated later, the array inside the element will be updated as well.
    To...


  23. » Marius
    I have an array of objects, and for plotting a graph I need to split it into 3 different arrays. How to do it using JS?

    const dataOverview = ;

    I...
    Last post » Admin
    It is very simple, you just use the map() function.

    var series1 = dataOverview.map(x => x.series1);
    var series2 = dataOverview.map(x =>...


  24. » Marius
    Why the function.call() behaves differently with and without ' this ', in JavaScript?
    The result with 'this' in test.call() is same when 'this' is...
    Last post » Admin
    The call() function require the first parameter as 'this' object, if you do not need it, just pass null .

    test.call(null, ...args);

    In your...


  25. » Marius
    I have the following problem:
    1. eliminate thousand separators(,) 5,555 to 5555.
    2. if the user inputs 55,66 replace , with . and get the value...
    Last post » Admin
    Try the function from the following code:

    function salNr(s){
    //If there is comma before the last two digit, replace it with dot, else remove...