0

I have been trawling around all day trying to fix this issue I am sure it is simple but I will be darned if I can figure it out.

I have tried the archive and cannot seem to find the solution to my particular issue, so any help will be very gratefully received!

I am wanting to add a style to an individual element when a list item is clicked. The list Item Id's and associated div classes are created dynamically in my php code.

With my script I have got as far as getting an alert box appearing as a test to show that the onclick event attached to the list item is returning the correct value. In this case ID and class "1995"

However when I add the correctly returned value into my script using

document.getElementsByClassName(supplyClass).style.display = "none";

In the console I get

"Uncaught ReferenceError: reply_click is not defined"

Abridged code is below with the succesful alert line commented out.

function reply_click(supplyClass) {
    //alert(supplyClass);
    document.getElementsByClassName(supplyClass).style.display = "none";
}
<div class="supply-container">
    <div class="supply-menu">
        <ul>
            <li id="1995" onClick="reply_click(this.id)">Desking Systems</li>
        </ul>
    <div>
    <div class="supply-content-container">
        <div class="1995 supply-content" >
                    <p>LorumIpsum</p>
        </div>
    </div>            
</div>

6
  • 2
    document.getElementsByClassName(supplyClass).style.display cannot work since getElementsByClassName returns a list of elements. However, the error means that reply_click is not defined, which could be the case if you have a syntax error in your function. The line itself us syntactically valid, so it might be somewhere else in the code that you didn't post. Commented May 11, 2018 at 18:34
  • Also reply_click(this.id) will return 1995 instead of classname Commented May 11, 2018 at 18:36
  • You should use document.querySelector('.' + supplyClass).style.display = 'none';, but that's unrelated to the error you are getting. Commented May 11, 2018 at 18:38
  • 1
    @FelixKling I think that will throw an error in this case since they are using a class name starting with a number. But that's a different issue too. Commented May 11, 2018 at 18:49
  • 1
    @Mark_M I think you are right: stackoverflow.com/questions/4089006/… Commented May 11, 2018 at 19:10

3 Answers 3

0

As your event handler is passing id of clicked element you need to use document.getElementById to find the element and make the display to none as below

    function reply_click(supplyClass)
{
    alert(supplyClass);
    //document.getElementsByClassName(supplyClass).style.display = "none";
    document.getElementById(supplyClass).style.display = "none";
}
<div class="supply-container">
    <div class="supply-menu">
        <ul>
            <li id="1995" onClick="reply_click(this.id)">Desking Systems</li>
        </ul>
    <div>
    <div class="supply-content-container">
        <div class="1995 supply-content" >
                    <p>LorumIpsum</p>
        </div>
    </div>            
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

Pretty sure the OP wants to find the element with class 1995.
@FelixKling i agree but as he has explicit id set for the ul element as well so i wasn't 100% sure
You are correct Felix and thanks both for your help.
0

document.getElementsByClassName(supplyClass) returns list of elements so you can't set its style directly.

If you want to set style for all elements returned this way then you can do it like this.

const els = document.getElementsByClassName(someClass);

for (let i = 0; i < els.length; i++) {
  els[i].style.display = 'none';
}

Comments

0

Thanks Felix & Sumeet The line of correct code that worked is as follows.

document.querySelector('.supply-' + supplyClass).style.display = 'none';

I had not realised you cannot start a class name with an integer so appended the word supply with a hyphen to the supplyClass value and it worked fine.

Thanks again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.