最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to set title attribute of button using jquery - Stack Overflow

programmeradmin1浏览0评论

I want to assign ajax return data to button's title attribute on hover of button, so i use $('#data_btn').title = html(returndata);, in below code, then it unable to show any output. Here, #data_btn is id of button element. Please help me regarding.

$('#data_btn').hover(function(){
var val_d = $('#country').val() +"-"+ $('#city').val();
window.alert(val_d);
if(val_d != 0)
	{
	$.ajax({
        type:'post',
		url:'getdata.php',
		data:{id:val_d},
		cache:false,
		success: function(returndata){
		    	$('#data_btn').title = html(returndata);											   
                       //$('#data_btn').html(returndata);
			  }
		  });
	    }
	})

I want to assign ajax return data to button's title attribute on hover of button, so i use $('#data_btn').title = html(returndata);, in below code, then it unable to show any output. Here, #data_btn is id of button element. Please help me regarding.

$('#data_btn').hover(function(){
var val_d = $('#country').val() +"-"+ $('#city').val();
window.alert(val_d);
if(val_d != 0)
	{
	$.ajax({
        type:'post',
		url:'getdata.php',
		data:{id:val_d},
		cache:false,
		success: function(returndata){
		    	$('#data_btn').title = html(returndata);											   
                       //$('#data_btn').html(returndata);
			  }
		  });
	    }
	})

Share Improve this question edited Apr 28, 2015 at 11:04 Shaunak D 20.6k10 gold badges47 silver badges79 bronze badges asked Apr 28, 2015 at 10:56 Vijaya SavantVijaya Savant 1354 silver badges19 bronze badges 6
  • 1 $('#data_btn').attr('title', html(returndata)); – Rahul Tripathi Commented Apr 28, 2015 at 10:57
  • I replaced my code with your, but not working for me. – Vijaya Savant Commented Apr 28, 2015 at 11:02
  • What does html() function do? Shouldn't that just be $('#data_btn').attr('title', returndata); – Shaunak D Commented Apr 28, 2015 at 11:03
  • Are you even getting into success: parameter?? – Dhaval Marthak Commented Apr 28, 2015 at 11:03
  • Arghhh! It's time for me to learn how to change baby nappies :) Step 1: get nappies. – lshettyl Commented Apr 28, 2015 at 11:06
 |  Show 1 more ment

3 Answers 3

Reset to default 3

if you want to use $('#data_btn').title = returnData;, do this: $('#data_btn')[0].title = returnData;, html() is undefined, so just assign returnData to your title

jsfiddle

by trying below code, it works for me.

$('#data_btn').attr('title',returndata);

You never use .title = html(returndata); the = sign in a jQuery chain... also, .title() is not a function.

Use the following:

$('#data_btn').attr('title', $(this).html(returndata));
发布评论

评论列表(0)

  1. 暂无评论