Skip to content Skip to sidebar Skip to footer

How To Save "title" Attribute Of "a" With Jquery?

How can I save the value of the title for a row? These are the values of the title=%s:

Solution 1:

Solution 2:

I think what you're trying to do is pass the value of the title attribute along in your AJAX request. If that's the case, the easiest thing to do will be to do it all in one event handler (is there a reason you're binding 2 different handlers to the same event?):

$("a.false").click(function(e) {
    $(this).closest("tr.hide").hide("slow");
    var main_id = this.title;
    var display = "false";
    e.preventDefault();
    $.ajax({
        url: "/useradminpage",
        data: {main_id: main_id, display: display},
        success: function(data) {
            display_false();
            alert("4 - returned");
        }

    });
});

Your problem currently is that main_id and display are not in the scope of the second event listener, so will be undefined (and they shouldn't be quoted, otherwise you're just passing in strings). As you're passing in a data object to the ajax function, you don't really need to add the query string to the URL either.

Aside from that, when you assign a value to main_id, you're using a.title. In this case a is undefined, and you will need to use this, which will be a reference to the clicked element.

Solution 3:

I suspect that I might be missing something, but I suspect that your problem is using a.title instead of this.title:

$("a.false").click(function(e) {
    $(this).closest("tr.hide").hide("slow");
    var main_id = this.title; // or you could use the jQuery object approach: $(this).attr('title') insteadvar display = "false";
    e.preventDefault();
});

The problem in your original approach is that a would be parsed as a variable, which hasn't been assigned a value, nor has it been declared, so that it would return undefined or null (at best). Within the scope of the each() method, you're iterating over individual nodes; so to access the properties/attributes of that node use this.

Solution 4:

To access any attribute of a DOM element through jQuery, you can use the .attr() function. In your particular case you would do.

var main_id = $(this).attr('title');

Post a Comment for "How To Save "title" Attribute Of "a" With Jquery?"