Jquery get custom attribute value

I have the following html part:

<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 store-id value. I have the following, but it does not work:

  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'm doing wrong.

+4
source share
3 answers

This works great:

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

http://jsfiddle.net/mHBuE/

+7
source

Take a look at jQuery custom selectors . Personally, I would use HTML5 data attributes for cases that are already supported in jQuery.

Whatever it takes, given the parameter that I believe you are trying to execute initially, should be executed as

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

all you have to do is:

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

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

+3
source

Source: https://habr.com/ru/post/1380661/


All Articles