2

In the initial part of my js code, I create element as below:

$('<input/>', {
               type: 'text',
               class: 'edit country',
               handle: '123456',
               name: 'customer'
               })

Then I add it to dom.

Sometime later, I want to retrieve the info stored in this element:

$(input_element).prop('handle') #which return 'undefined'
$(input_element).attr('handle') #which give me '123456'

$(input_element).prop('name') #which return 'customer'
$(input_element).attr('name') #which give me 'customer'

I want to know what is wrong with my prop() method for fetching 'handle'? Is that because 'handle' is not standard HTML attribute?

I have read this post .prop() vs .attr() No big help.

Anyone have a clue?

Community
  • 1
  • 1
Jerry Meng
  • 1,466
  • 3
  • 21
  • 40
  • 2
    This post will help - http://stackoverflow.com/questions/6003819/properties-and-attributes-in-html – Jay Blanchard Apr 24 '14 at 15:42
  • 5
    Attributes are not properties. Most of the standard HTML attributes are mapped to properties, but custom attributes aren't. Also keep in mind that custom attributes should be prefixed by `data-` for your document to be valid. – Frédéric Hamidi Apr 24 '14 at 15:50
  • 7
    ↑↑↑ Like said, you should use: `"data-handle": '123456',` and then you could use: `$(input_element).data('handle')` – A. Wolff Apr 24 '14 at 15:51
  • @JayBlanchard, thanks, that link helps – Jerry Meng Apr 24 '14 at 16:01
  • 1
    @FrédéricHamidi, thanks for explanation, I have a better understanding on properties and attributes now. – Jerry Meng Apr 24 '14 at 16:02
  • Does this answer your question? [.prop() vs .attr()](https://stackoverflow.com/questions/5874652/prop-vs-attr) – depperm May 11 '23 at 13:23

0 Answers0