javascript - Can't get the id name using onClick? -


http://wthdesign.net/test/test.html

what i'm trying append id name url, i'm getting "#undefined" instead?

the script i'm using:

function generateurl() {     var currentid = $(this).attr('id');     document.location.hash = currentid;     console.log($(this)); } 

inside html:

<a id="abc" onclick="generateurl()" >this anchor btn</a> 

if debug, you'll find this in context window object. can pass this function so:

function generateurl(el) {     var currentid = $(el).attr('id');     document.location.hash = currentid; } 
<a id="abc" onclick="generateurl(this)" >this anchor btn</a> 

alternatively, can use jquery replace inline onclick so:

$("#abc").click(function() {     var currentid = $(this).attr('id');     document.location.hash = currentid; } 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -