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> 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> 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
3 Answers
Reset to default 8This 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)