Hai Truong IT
Hai Truong IT

Reputation: 4187

How to get href="" in <a> using jquery?

I have a code:

<div id='basic-modal'>
<a href='index.php?option=com_atm&view=showdistrict&layout=map&tmpl=component&reset=1' class='basic'>[VIEW]</a>
</div>

and script:

<script type='text/javascript'>
$(document).ready(function(){
    $('#basic-modal .basic').click(function (e) {
        $('<div></div>').load('link href').modal();
        return false;
    });
});
</script>

How to get url from href "index.php?option=com_atm&view=showdistrict&layout=map&tmpl=component&reset=1" of tag ?

Upvotes: 0

Views: 286

Answers (3)

nikc.org
nikc.org

Reputation: 16993

Using jQuery, you can get attribute values using .attr(). In your example, however, you can use native JavaScript's element.getAttribute.

Jquery conveniently binds event handlers to the right context (meaning, this refers to what you expect it to), i.e. you can simply say this.getAttribute('href'). If you really insist on using jQuery, you can say $(this).attr('href'), but that's just wasting resources.

<script type='text/javascript'>
$(document).ready(function(){
    $('#basic-modal .basic').click(function (e) {
        $('<div></div>').load(this.getAttribute('href')).modal();

        return false;
    });
});
</script>

Upvotes: 1

rabudde
rabudde

Reputation: 7722

try

alert($(this).attr("href"));

inside click-function you can access every attribute of an element by jQuerys attr function and you can also set or modify an attribute by providing a second parameter.

Upvotes: 1

King Friday
King Friday

Reputation: 26086

cleaned up a bit

<script type="text/javascript">
$(function(){
    $('#basic-modal .basic').click(function (ev) {
        ev.preventDefault();
        $('<div />').load($(this).attr('href')).modal();
    });
});
</script>

Upvotes: 0

Related Questions