Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Javascript Remove attr

I'm pretty sure I can do this in Javascript but when a 'button' is clicked I would like it to remove some CSS Ids.

I havent started the Javascript course but I need to do this ASAP :-)

5 Answers

Hi Shaun,

you can do this easily using jQuery. Are you familiar with jQuery or looking for an pure JS solution? For jquery check this out: https://api.jquery.com/remove/

Well I wasen't sure If I could do this with pure CSS

After looking at the link can you do this:

onclick= $varible( ".content_overlay" ).remove();

For pure JS you could use:

element.removeAttribute(attrName);

Can you post some of your code? That would help speed it up

<div onclick="$('.content_overlay').remove();" id="view-button" >
                View <?php echo $heading_title  ?>
            </div>

I am finding it difficult to understand... .content_overlay is not an ID, it is a class. Why are you removing it? I assume you want to remove the ID #view-button?

If so try this and remove the 'onclick="$('.content_overlay').remove();"' part:

onclick = function() {
   this.removeAttribute(id);
}

Does this work?

Sorry the Content_overlay is an ID so its #content_overlay and when I click #view-button I want the #content_overlay to be removed

Okay, then try this:

onclick = function() { document.getElementById(content_overlay).removeAttribute(id); }

So I added this in:

<div onclick = function() { document.getElementById(content_overlay).removeAttribute(id); } id="#view_button" >
                View <?php echo $heading_title  ?>
            </div>

And the button doesn't show up now

#view_button{
    background-color: #666;
    box-shadow: 2px 2px 6px 0px rgba(102,102,102,0.64);
    text-align: center;
    font-family: "'QuattrocentoRomanRegular'";
    font-size: 25px;
    color: white;
    padding-top: 20px;
    margin:300px auto;
    padding-bottom: 20px;
    height: auto;
    width: auto;
    }

that is waht you wanted right?

When I click the button I want the #content_overlay removed/gone

So I am now getting this error:

Uncaught SyntaxError: Unexpected token (

Any answers? on getting this error

Uncaught SyntaxError: Unexpected token (