3

I'm doing something very simple, like this:

<div class='item'>HI I'm an item</div>

.item:hover:after {
content: "Delete";
}

What i want to do is when the user hovers over the div, for the word Delete to appear and have it possible that when the user clicks on the word Delete, for me to run some jQuery (to actually remove this item)

Is this possible?

3
  • 1
    +1 for a good question. I don't know the answer. However I would suggest that it's not a good idea to do this even if it does work, because if it's clickable then it isn't there just for styling, so it doesn't belong in the stylesheet. Commented Sep 15, 2011 at 9:22
  • Yes, i just wanted to avoid extra markup with spans etc... Commented Sep 15, 2011 at 9:23
  • Pseudo-elements are not part of the DOM, so... Commented Sep 15, 2011 at 13:46

3 Answers 3

3

That's just not possible, sorry. Pseudo-elements are purely for presentation purposes.

Yes, i just wanted to avoid extra markup with spans etc...

For something as important as a "Delete" link, the proper place for this to go is in the HTML.


If you really don't want this in the HTML (maybe you have extenuating circumstances), you might as well just do it using a little more jQuery:

$('<span class="del">Delete</span>').appendTo('.item').click(function() {
    alert('deleted');
});

.del {
    display: none;
}
.item:hover .del {
    display: inline;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I too agree. If you need to attach handlers to <span class="del" /> Then you definitely need to go with jQuery for adding and removing it from the DOM.
a delete button's proper place isn't necessarily in the markup, it wont work without JS or page reload; adding it via JavaScript is more optimal. if you want your delete buttons to indexed by Search Engine bots, then yes it should be present in the markup, but i cannot think of a reason that would compel you to do so. i'm not saying you're never going to want them indexed, but more than likely 99% of the time. furthermore, if you are putting them in the markup and following Web Standards then the button should still functionally work without JavaScript, which means more work ensuring that.
Either way, the reason why you can't implement this with pseudo-elements is because those things are purely presentational.
2

That's not possible. You can accomplish the desired results in this way: CSS:

.item > .del {
    /* "A > B" means: Select element B which is the direct child of A.
     * You surely don't want all nested .del elements to collapse. */
    width: 100px;
    height: 30px;
    display: none;
    /*Additional styles*/
}
.item:hover > .del{
    display:inline-block;
}

HTML:

<div class="item">
   Blabla
   <div class="del" onclick="doSomething()">Delete</div>
</div>

1 Comment

You should use .del instead of #del. It's likely that there are multiple .items.
0

you can totally do that, although i'm not sure about "delete"....do you mean just take it out of the DOM? if so, here's your solution. http://jsfiddle.net/jalbertbowdenii/UVxWq/

ok, here's the solution for your specification: http://jsfiddle.net/jalbertbowdenii/UVxWq/ delete shows up on hover and onclick deletes the .item. no extra markup.

4 Comments

Sort of the right idea, but i need it to action the jQuery when the user specifically clicks the delete, not the whole element
i don't follow what you mean by "action the jQuery"
@albert: He's saying he wants "to run the JavaScript/jQuery only when you click the delete link, not when you click any of the rest of the element" (in your solution, clicking any part of the element runs the code).
@thirty lolz. i'm so dumb. yeah i got the specification, just was thrown off by "action the jQuery"...my bad. thx.

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.