I have this HTML element:
<div id='4-12-2012' class='date'>blah blah blah</div>
And I'm binding a click event to .date, so when it's clicked, a function is called. My goal is to end up with the date (from the ID) with slashes instead of dashes:
d = $(this).attr('id').replace('/-/g', '/');
alert(d);
This can't be much more straightforward... but the result that is displayed with the alert() still has dashes, "4-12-2012"... what totally obvious thing am I missing here?! :-)
The answer to your question has already been posted, but on a side note, I see no reason to use jquery in this situation. All it is doing is calling un-needed functions
try this
d = this.id.replace(/-/g, '/');
here is a good reference with testing:
http://jsperf.com/el-attr-id-vs-el-id/7
edit: forgot to remove the ticks
try this :
remove the '
around the regex
d = $(this).attr('id').replace(/-/g, '/');
Simple regexp, as seen on this jsfiddle:
d = $(this).attr('id').replace(/-/g, '/')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With