removing a div element with coffeescript - dom

I want to remove a div element with a specific class attribute using Coffeescript. I couldn't find any examples about DOM manipulation with Coffeescript on the Internet. How can I do this? Also any references to doing DOM would be great.

CoffeeScript is a JavaScript preprocessor, there is no additional standard library. What this means is that if you want to do DOM manipulation you would do it the same way you would in JavaScript.
You can use any JavaScript library like jQuery with CoffeeScript, alternatively you can use the document variable directly:
element.parentNode.removeChild(element) for element in document.getElementsByClassName('some-class')
Or (for browsers not supporting that method)
element.parentNode.removeChild(element) for element in document.getElementsByTagName('*') when element.className = 'some-class'
Or, since those identifiers are somewhat long, use block syntax:
for element in document.getElementsByTagName('*')
if element.className is 'some-class'
element.parentNode.removeChild(element)
Relevant quote from CoffeeScript.org:
The golden rule of CoffeeScript is: "It's just JavaScript". The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa).

#lauren's answer works for me but when I'm using chrome, I'm getting the following error:
Uncaught TypeError: Cannot read property 'id' of undefined
Using the following works perfect, tested on Chrome.
$(document).on 'hidden.bs.modal', "#newProject", ->
document.getElementById("<ID>").outerHTML=''
delete element
From:
https://stackoverflow.com/a/19298575/5452072

Related

Is there any requirement for DOM used to create a DominoTree, such as being namespaceaware or created with level 2 dom builder?

We are adding code to convert a DOM to Domino before invoking saxon library to process xquery expression, which involves constructing a xml document as output.
Following exception is seen, for DOM created using certain DOM builders:
java.lang.NullPointerException
at net.sf.saxon.om.NameOfNode.equals(NameOfNode.java:177)
at net.sf.saxon.om.SingletonAttributeMap.put(SingletonAttributeMap.java:69)
at net.sf.saxon.om.NodeInfo.attributes(NodeInfo.java:528)
at net.sf.saxon.tree.util.Navigator.copy(Navigator.java:673)
at net.sf.saxon.om.NodeInfo.copy(NodeInfo.java:568)
at net.sf.saxon.tree.util.Navigator.copy(Navigator.java:679)
at net.sf.saxon.om.NodeInfo.copy(NodeInfo.java:568)
at net.sf.saxon.event.ComplexContentOutputter.decompose(ComplexContentOutputter.java:860)
at net.sf.saxon.event.ComplexContentOutputter.append(ComplexContentOutputter.java:656)
The cause seems to be that node.getLocalName() for attribute type of nodes, returns null for Domino. Same setup works if DOM is converted to Tiny Tree or passed using a DOMWrapper to Saxon.
Xquery using domino, expected to work, threw Exception.
You've raised this on the Saxonica support forum at https://saxonica.plan.io/issues/5727
To summarise:
(a) Whether using the DOM wrapper in Saxon, or the Domino structure (which adds extra indexing), the DOM should be namespace-aware and should be constructed using namespace-aware (i.e. level-2) interfaces.
(b) If you don't follow that rule, we shouldn't crash (so we'll fix the crash that you've observed), but we can't guarantee delivering XPath-conformant results. The mapping from a non-namespace-aware DOM to a valid XDM instance is undefined and unpredictable, so the results of XPath processing are undefined. This might mean, for example, that when the result is serialized, it's not well-formed XML (for example, it might contain undeclared prefixes in element or attribute names.

Difference between protractor locators

I was going through the protractor guide here: https://github.com/angular/protractor/blob/master/docs/api.md#api-protractor
It says in order to locate a element, I could use
var temp = element(by.css("someclass"));
or alternatively
var temp1 = ptor.findElement(protractor.By.css('someclass'))
Which kind of locator is to be used when ? Could someone pls clarify
They are the same. element is the preferred syntax because it is shorter and because you can chain locators and use some fancy protractor features. Protractor extends the webdriver api and that is why you can use the same functions that you would use in plain webdriver.
For example, the following expressions are equivalent:
ptor.findElement(by.css('.foo')).getText()
element(by.css('.foo')).getText()
$('.foo').getText()
To look for multiple elements use:
ptor.findElements(by.css('.foo'))
element.all(by.css('.foo'))
$$('.foo')
There are many examples in the api.md document:
https://github.com/angular/protractor/blob/master/docs/api.md#elementfinderprototypeelement
The difference between ptor.findElement and element is that the first should be used pages without Angular while the second is used on pages with Angular. This is related to how protractor syncs with Angular. The first returns a WebDriver WebElement, the second returns a Protractor ElementFinder.
However to address your question directly, there is no difference between the locators returned by by.css and protractor.By.css.
The two are equivalent. The object referenced by the global by object is the same as the the object referenced by protractor.By.
From Protractor's runner.js:
global.by = global.By = protractor.By;
There are two versions of the API. The old version used protractor.By, whereas the new version uses by. You may often see the old style but if you are in doubt, you can use the new style and be sure nothing unexpected will happen.

How do I associate a CoffeeScript file with a view?

Just installed rails 3.1 rc1 and am trying to grok the best way to manage javascript with the new asset pipeline
By default all coffeescript is compiled into a single application.js file, this is a good thing.
Each seperate coffee script file is appended to the js file and wrapped in an anonymous function which is executed via the call method
A common scenario would be to use some jquery to turn various forms into ajax forms, update UI, etc...
Many of these scripts will be specific to a controller or action, I am trying to grok the 'conventional' way to handle this,
since everything is wrapped in an anonymous function how do I only execute just
the code for a particular controller / action, by default all of the anonymous functions are being executed
I did play around with some hacks where I load the controller and action name into js variables and then in
coffeescript check those to conditionally run code, I don't like that very much
my initial thought was that each coffee file would contain a js namespace/object and I would call the specific ones from the view,
going to spike this using the default_bare = true configuration
see How can I use option "--bare" in Rails 3.1 for CoffeeScript?
EDIT
Looking around some more: this looks like it might be the correct approach - "Can't find variable" error with Rails 3.1 and Coffeescript
There are two common approaches:
Make behavior conditional on the presence of a particular element. For instance, code to run a signup sheet should be prefaced with something like
if $('#signup').length > 0
Make behavior conditional on a class on the body element. You can set the body class using ERB. This is often desirable for stylesheets as well. The code would be something like
if $('body').hasClass 'user'
gistyle is a simple gem that helps you running action-specific javascript codes.
By following its setup, you set some data attributes in your body element, representing the current controller and action names. Then it will only call that action when the corresponding view is loaded.

Javadoc on CoffeeScript?

I'm new to CoffeeScript and seems that I can't find any document generator for CoffeeScript using Javadoc syntax. The only one I could find is available as a patch to the CoffeeScript compiler.
So, what do you use to generate documentation from Javadoc comment on CoffeeScript or how do you document your function arguments and return values?
So, JavaDoc syntax has never really caught on with JavaScript developers. There are those who use something like it—notably Google—but it's kind of at odds with JS, which doesn't have static typing and allows any number of arguments to any function.
If you want to create beautiful documentation with CoffeeScript, the standard is Docco (its home page is an excellent example). If you want to create JavaDoc-style comments for every function... well, you'll have to create them by hand, and escape them with backticks.
Alternatively, you could work in CoffeeScript until your code is release-ready, then document the resulting JavaScript.
Docco is great for prozedural coding style. If you want to document an API, coffeedoc is for you.
People looking forward to using javadoc style documentation in coffeescript can checkout codo ( http://netzpirat.github.com/codo/ ) which provides support for a subset of javadoc and automatically infers class names, function names and parameters from source code.
I'm using YUIDoc. I can add comments using a syntax similar to Javadoc to my classes, methods and events. The documentation gets output as html/css files and you can even customize the page layout.
Check this documentation example: http://yui.github.com/yuidoc/api/
PS: It relies on Node.JS and you need to install the package yuidocjs
npm install yuidocjs -g

Non-trivial screen scraping selections using pQuery

I'm using pQuery (a Perl port of jQuery) to select elements and retrieve text from a HTML-document.
Consider the following markup:
<x>
<y>code1</y>
<z>stuff</z>
<y>code2</y>
<z>foobar</z>
</x>
And the following pQuery code:
my $target_value = pQuery($markup)->find($pquery_selector)->text;
I'm trying to formulate $pquery_selector so that it matches <z>foobar</z> in the markup above using the following rule: find the z-element that follows after a y-element which has a body containing "code2". While this is possible using jQuery I'm not sure that the pQuery syntax is powerful enough to handle such an expression.
Is this type of selection possible using the pQuery syntax?
In jQuery it might be possible to write a selector like 'y:contains(code2)+z'. However, pQuery is still unfinished (as of version 0.07), and a selector like x+z just gives an error demonstrating that the module developer hasn't gotten around to translating that part of the jQuery code.
Since pQuery hasn't been touched since 2008, I'd recommend either fixing it yourself (the code is on cpan and github), or using a more mature module like HTML::TreeBuilder::XPath (which does require learning XPath syntax, but actually works for non-trivial things).
The XPath equivalent of the above jQuery selector would be '//y[contains(text(), 'code2')]/following-sibling::z'