jq beginner
jq beginner

Reputation: 1070

very simple code to change attribute but not working

i'm trying to change the title attribute of a div when clicking on a link but it's not working here's the code

<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(e) {
        $("a#change").click(function() {
            $("#city").attr("title", "new title");          
        }); 
    });
</script>

and the body :

<a href="#" id="change">change</a>
<div id="city" title=""></div>

Upvotes: 0

Views: 369

Answers (2)

Talha Akbar
Talha Akbar

Reputation: 10040

$(document).ready(function(e) {
    $("a#change").click(function(e) {
        e.preventDefault();
        $("#city").attr("title", "new title");           
    }); 
});

Working Fiddle

If you want to change any property of element, you can use .attr() or .prop(). But here i used .attr().

For More Examples See Documentation:

Documentation

Upvotes: 3

Koenyn
Koenyn

Reputation: 704

try one of the following lines of code:

// this will change the content of the div itself
$("#city").html("new title");

// this will change the title attribute's value
$("#city").attr("title", "new title");

Upvotes: 1

Related Questions