How to save title attribute for "a" using jQuery?

How to save header value for string? These are the title=%s: values title=%s:

 <a class="false" title=1106 href="/useradminpage?main_id=%s&display=false"><span class="small">(hide)</span></a> <a class="false" title=1153 href="/useradminpage?main_id=%s&display=false"><span class="small">(hide)</span></a> <a class="false" title=1175 href="/useradminpage?main_id=%s&display=false"><span class="small">(hide)</span></a> 

...

I tried countless variations, but none of them work. This is what I have now:

 <script> $(document).ready(function() { console.log("ready"); $("a.false").click(function(e) { $(this).closest("tr.hide").hide("slow"); var main_id = a.title; var display = "false"; e.preventDefault(); }); $("a.false").click(function() { $.ajax({ url: "/useradminpage?main_id=%s&display=false", data: {main_id: "main_id", display: "display"}, success: function(data) { display_false() alert("4 - returned"); } }); }); }); </script> 

This is the third question on this topic. I appreciate any help. Thanks.

0
source share
4 answers

I think you are trying to pass the value of the title attribute in your AJAX request. If so, the easiest way to do this is in one event handler (is there a reason why you are linking 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"); } }); }); 

Currently, the problem is that main_id and display not in the scope of the second event listener, so it will be undefined (and they should not be specified, otherwise you just pass the lines), since you are passing the data object to the ajax function, you really don't need to add URL query string.

Also, when you assign the value to main_id , you use a.title . In this case, a is undefined, and you will need to use this , which will be a link to the clicked item.

+2
source

instead

  var main_id = a.title; 

to try

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

because if I'm not mistaken, "a" is not defined

+5
source

I suspect I'm missing something, but I suspect 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') instead var display = "false"; e.preventDefault(); }); 

The problem with your original approach is that a will be parsed as a variable that has not been assigned a value and not declared to return undefined or null (at Best). Within the each() method, you iterate over individual nodes; therefore, use this to access the properties / attributes of this node.

+2
source

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

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

All Articles