Welcome Guest, Not a member yet? Register   Sign In
[solved] jquery how to get attribute of clicked link?
#1

[eluser]SPeed_FANat1c[/eluser]
Here is the link.
Code:
<a class="del" href="" filename="naujiena11.jpg"><img src="http://localhost/darzelis/images/admin/delete.png"></a>

It has class "del" and attribute "filename".

Code:
$('a.del').live('click',function(){
        
    console.log //I want to get filename
    
         return false;
     });
How can I do it? Tried to google, simple thing but no results yet Sad
I cannot use $('a.del') to select, because there is more links with the same class, I want to select which is clicked.
#2

[eluser]Eric Barnes[/eluser]
Well for one filename is not a valid attribute to the a tag so I do not believe that will even work. As a workaround you could do something like:
Code:
<a class="del" href="" id="file_1"><img src="http://localhost/darzelis/images/admin/delete.png"></a>  
&lt;input type="hidden" id="filename_1" name="filename" value="naujiena11.jpg" /&gt;
$('a.del').live('click',function(){
    var id = this.id.replace('file_', "");
    console.log($('#filename_'+id).val());
    return false;
});
#3

[eluser]SPeed_FANat1c[/eluser]
I use values from id's but this time decided to make my own attribute so it would look nice Smile So I guess I'll have to use id. Thanks. Smile
#4

[eluser]cideveloper[/eluser]
I understand this is a solved topic but just to clarify things you can now start using custom data atrributes according to the HTML5 specs. So you can use an attribute like data-filename. The custom attribute must be prefixed with "data-"

Read more here.

that way you could use

Code:
$('a.del').live('click',function(e){
         e.preventDefualt();
         console.log($(this).attr("data-filename"));
});
#5

[eluser]SPeed_FANat1c[/eluser]
There is absolutely no problem that it is solved, thank you Smile
#6

[eluser]Eric Barnes[/eluser]
@progra@mmer - Any idea if that is supported in most browsers? Or is that only available to newer news? Specifically ie6/ie7?
#7

[eluser]cideveloper[/eluser]
I have tries it in IE7, IE8, IE9 Beta, various flavors of FF, and Chrome with no problems. I have stopped supporting IE6 and do not even have access to that browser. I know this may sound elitist but people need to upgrade their ancient browsers and I tell them that. I mean really that browser was released August 27, 2001.
#8

[eluser]Eric Barnes[/eluser]
Yea I don't disagree. Just curious for the most part. Smile




Theme © iAndrew 2016 - Forum software by © MyBB