0

Please refer to me a reference or documentation that explain these kind of selection using tag, text and class.

var button_var = $('<button/>',
       {
           text: 'Click',
           class: 'btn_class'

       });
zac
  • 4,495
  • 15
  • 62
  • 127

3 Answers3

1

Try this reference for the specific jQuery method: http://api.jquery.com/jquery/#jQuery-html-attributes

That is not a selection. That is creating a <button> DOM element with properties specified by the anonymous object passed as the second parameter.

Note: until the object is added to the DOM somewhere it is a disconnected element.

JSFiddle: http://jsfiddle.net/TrueBlueAussie/zr9tdLv1/

A cut & paste of the resulting DOM element (from Chrome DOM inspector is)

<button class="btn_class">Click</button>

Update: (thanks to mention by @Don P)

As class is to become a reserved word, you should quote the key name. e.g.

'class': 'btn_class'

JSFiddle: http://jsfiddle.net/TrueBlueAussie/zr9tdLv1/1/

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
  • I do not believe you can use class in all browers as you have it, you would have to pass the key as a string. – Don P Jan 23 '15 at 22:38
  • @Don P: Unless there are special characters in the name the "key" does not have to be quoted. – iCollect.it Ltd Jan 23 '15 at 22:44
  • Do I must use tag ? I tried – zac Jan 23 '15 at 22:54
  • but we create the button like this !!? – zac Jan 23 '15 at 22:56
  • 1
    You can use any of those 3 formats. It is smart enough to figure out what you are creating. The self-closing syntax is the simplest shortest "correct" one if you are not adding child elements. – iCollect.it Ltd Jan 23 '15 at 23:05
  • @TrueBlueAussie please reference http://stackoverflow.com/questions/7524618/why-is-class-a-reserved-word-in-javascript – Don P Jan 24 '15 at 00:00
1

It's just creating the element button with html attributes and you'll be able to see the final result like below:

<button class="btn_class">Click</button>

So, by the way you can create the element with the attributes (HTML DOM Properties) all html have like id, data-* attributes. You can also have html as attribute and use there html like html: '<div>foo</div>'


Thanks to @TrueBlueAussie, found the doc

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • thanks, no documentation explain creating element in more detail ? like other options for example ? – zac Jan 23 '15 at 22:41
  • I see thanks, it is first time I see code like this so I am trying to understand. Why is used not " – zac Jan 23 '15 at 22:44
  • 1
    It's the way we do to create the element `` that is just a short hand... – Bhojendra Rauniyar Jan 23 '15 at 22:47
-1

I guess you are trying to achieve this

$("button.btn_class[value='Click']")
Amal Ts
  • 857
  • 1
  • 6
  • 28