With Polymer 1.0 - do we still need jquery?

159 views
Skip to first unread message

Jim Wang

unread,
Nov 17, 2015, 8:57:32 PM11/17/15
to Polymer
In jquery, we could control html elements like: $("button[name^='mybutton']"). then do something about it.
With Polymer 1.0 - do we still need jquery? Can Polymer do all jquery's work?

Cheers!

Justin Fagnani

unread,
Nov 17, 2015, 9:41:05 PM11/17/15
to Jim Wang, Polymer
On Tue, Nov 17, 2015 at 5:57 PM, Jim Wang <jian...@gmail.com> wrote:
In jquery, we could control html elements like: $("button[name^='mybutton']"). then do something about it.
With Polymer 1.0 - do we still need jquery? Can Polymer do all jquery's work?

You don't need jQuery, or Polymer for that, just do: document.querySelector("button[name^='mybutton']")

Within a Polymer element we have a help for finding elements inside the local dom: this.$(id) and this.$$(selector), which are roughly equivalent to this.shadowRoot.querySelector('#id') and this.shadowRoot.querySelectorAll(selector).

Since ids are useful again (because they're scoped to a shadow root), I'd recommend using this pattern when you can:

<template>
  <button id="mybutton">Click Me</button>
</template>
<script>
  ...
  doSomething(this.$.mybutton);
</script>

Of course, Polymer also adds a bunch of helpers for declaratively adding event listeners, data-binding to values, etc., so many times you might not even need to get a reference in code.


Cheers!

Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/672d8255-7f87-4218-92cb-7b50fe543e81%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages