2

My code works if it is hardcoded with LLC. However, I would like to pass the variable and do not know how to modify the code to accept variable. This is related to Zendesk Guide customization.

For this line:

https://XYZ.zendesk.com/api/v2/help_center/articles/search.json?label_names=LLC',

I would like to replace LLC with a variable value that is passed from HTML.

See code below:

<div>
    {{#if article.labels}}
    {{#each article.labels}} 

    <p>Articles related to <b> {{identifier}} :</b></p>
        <ul class="related">
        <p id="Project"></p>
    </ul>

    {{/is}} 
    {{/each}}
    {{/if}}

    <script type="text/javascript">
        <!-- create elements for LLC articles -->

        function createNode(element) {
          return document.createElement(element);
        }

        function append(parent, el) {
         return parent.appendChild(el);
        }

        <!-- list LLC articles-->

        const ul = document.getElementById('Project');
        const url = 'https://XYZ.zendesk.com/api/v2/help_center/articles/search.json?label_names=LLC';

        fetch(url)
            .then((resp) => resp.json())
            .then(function(data) {

        let family = data.results;

        return family.map(function(article) {
            let li = createNode('li'),
            span = createNode('span');
            span.innerHTML = "<a href ="+`${article.html_url}`+">"+`-    ${article.title}`+"</a>";
            append(li, span);
            append(ul, li);
            })
        })

            .catch(function(error) {   
                console.log(error);
            });
        }
    </script>

From the above HTML, the value of LLC is retrieved from {{identifier}}. I need to pass the value of {{identifier}} to the JavaScript code to replace LLC. How could I achieve this?

2
  • by using window.location.search? Commented Apr 24, 2019 at 18:15
  • Also, don't mix template literals and string concatenation. Do this instead: span.innerHTML = `<a href="${article.html_url}">${article.title}</a>`; Within template string you can put literal strings, and only the parts within ${} are interpolated as expressions. Commented Apr 24, 2019 at 19:41

1 Answer 1

1

You you have the LLC variable available, then it is just a matter of simple string concatenation.

const LLC = "something";
const url = 'https://XYZ.zendesk.com/api/v2/help_center/articles/search.json?label_names=' + LLC;

You can also use template string literals if there is support for ES6.

const LLC = "something";
    const url = `https://XYZ.zendesk.com/api/v2/help_center/articles/search.json?label_names=${LLC}`;
Sign up to request clarification or add additional context in comments.

2 Comments

...and if you don't already know how to, you can get the innerHTML content of an HTML element by first selecting the element it from the DOM. The simplest way is if you can isolate your content in an element with anid attribute, something like <p>Articles related to <b><span id="identifierElement"> {{identifier}}</span> :</b></p>, which allows const myElement = document.getElementById("identifierElement"); const LLC = myElement.innerHTML; to work. But Element.querySelector (developer.mozilla.org/en-US/docs/Web/API/Element/querySelector) is nice too.
How can I pass 2 variables instead of 1?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.