最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery get a custom (made up) Attribute value - Stack Overflow

programmeradmin2浏览0评论

I have a the following html piece :

<div onClick="javascript:getComments(this);" store-id="143568" class="CountryRow" style="padding: 4px;"><div class="flag flag_az"></div> &nbsp; Azerbaijan</div>

and I would like to create a jquery function to get the value of store-id. I have the following however its not working :

 getComments = function(input) {
   fValue = $(input).val( $(input).attr("store-id") );
   alert('the ID :'+fValue);
 }

can someone be kind enough to tell me what it is that I am doing wrong.

I have a the following html piece :

<div onClick="javascript:getComments(this);" store-id="143568" class="CountryRow" style="padding: 4px;"><div class="flag flag_az"></div> &nbsp; Azerbaijan</div>

and I would like to create a jquery function to get the value of store-id. I have the following however its not working :

 getComments = function(input) {
   fValue = $(input).val( $(input).attr("store-id") );
   alert('the ID :'+fValue);
 }

can someone be kind enough to tell me what it is that I am doing wrong.

Share Improve this question edited Oct 27, 2016 at 6:05 Jacopo Penzo 2,1682 gold badges24 silver badges29 bronze badges asked Nov 10, 2011 at 22:13 Ahoura GhotbiAhoura Ghotbi 2,89613 gold badges38 silver badges65 bronze badges 1
  • For what it's worth, HTML5 provides a standard facility for custom attributes like that. ejohn.org/blog/html-5-data-attributes – mwilson Commented Nov 10, 2011 at 22:16
Add a comment  | 

3 Answers 3

Reset to default 8

This works perfectly:

getComments = function(input) {
  fValue =  $(input).attr("store-id");
  alert('the ID :'+fValue);
}

http://jsfiddle.net/mHBuE/

Take a look at jQuery custom selectors. Personally, I would use HTML5 data attributes for cases such as this which is already supported in jQuery.

For whatever it's worth, considering the parameter I believe what are you trying to originally perform should be done like

getComments = function(input) {
   fValue =     $(input).html( $(input).attr("store-id") );
   alert('the ID :'+fValue.html());

   }

all you have to do is :

fValue = $(input).attr("store-id");

your snippet is trying to add to the 'value' attribute of a div (which does not exist)

发布评论

评论列表(0)

  1. 暂无评论