How to delete from DOM

105 views
Skip to first unread message

Sean Neilan

unread,
Feb 19, 2012, 5:15:41 PM2/19/12
to Enlive
Hi.

Using at and content, I can replace the content of whatever I've
selected with nothing.

For example:
(require '[net.cgrand.enlive-html :as html])

(-> "<html><head><title>asdf</title><body><div id='derp'><a href='#'>I
am a link.</a></div><div id='anotherLink'>moar content</div></body</
html>"
java.io.StringReader.
html/html-resource
(html/at #{[:#derp]} (html/content "")))

Now, div#derp contains nothing instead of the link.


My question is: What if I want to delete div#derp altogether from the
DOM?

Thank you for your time.

Nandan Bagchee

unread,
Feb 19, 2012, 9:37:04 PM2/19/12
to enliv...@googlegroups.com

Instead of content, use a fn whose return value is nil.

--
You received this message because you are subscribed to the Google Groups "Enlive" group.
To post to this group, send email to enliv...@googlegroups.com.
To unsubscribe from this group, send email to enlive-clj+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/enlive-clj?hl=en.

Sean Neilan

unread,
Feb 19, 2012, 10:48:00 PM2/19/12
to enliv...@googlegroups.com
(-> "<html><head><title>asdf</title><body><div id='derp'><a href='#'>I am a link.</a></div><div id='anotherLink'>moar content</div></body</html>"
 java.io.StringReader.
 html/html-resource
 (html/at #{[:#derp]} (fn [x] nil)))

That works!

Thank you very much!

Nandan Bagchee

unread,
Feb 19, 2012, 10:56:09 PM2/19/12
to enliv...@googlegroups.com
If you *always* need to remove that div, you can also use nil directly.

user> (-> "<html><head><title>asdf</title></head><body><div id='derp'><a href='#'>I am a link.</a></div></body</html>"
   java.io.StringReader.
   html/html-resource
   ;; either  (html/at #{[:#derp]} (fn [& args] (when (derp-day) identity)))
   ;; or
   (html/at #{[:#derp]} nil))

=>

({:tag :html, :attrs nil, :content ({:tag :head, :attrs nil, :content ({:tag :title, :attrs nil, :content ("asdf")})} {:tag :body, :attrs nil, :content ({:tag :div, :attrs {:id "anotherLink"}, :content ("moar content")})})})

Reply all
Reply to author
Forward
0 new messages