javascript - get this anchor title text jQuery - Stack Overflow

I'm trying to get all anchor title text inside children div'sMy HTML<div onClick="ope

I'm trying to get all anchor title text inside children div's

My HTML

<div onClick="openList();">
  <div class="inc">
    <div class="iWrap">
        <a title="<div>blah1 blah1</div>"></a>
        <a title="<div>blah2 blah2</div>"></a>
        <a title="<div>blah3 blah3</div>"></a>
        <a title="<div>blah4 blah4</div>"></a>
    </div>
  </div>
</div>

<div onClick="openList();">
  <div class="inc">
    <div class="iWrap">
        <a title="<div>some text 1</div>"></a>
        <a title="<div>some text 2</div>"></a>
        <a title="<div>some text 3</div>"></a>
        <a title="<div>some text 4</div>"></a>
    </div>
  </div>
</div>

My jQuery code

function openList()
{
    var getTitletxt = $(this).find('a').attr("title");
    console.log(getTitletxt);
}

I'm getting undefined in my console.log. Basically i'm trying to fetch text inside title div.

I'm trying to get all anchor title text inside children div's

My HTML

<div onClick="openList();">
  <div class="inc">
    <div class="iWrap">
        <a title="<div>blah1 blah1</div>"></a>
        <a title="<div>blah2 blah2</div>"></a>
        <a title="<div>blah3 blah3</div>"></a>
        <a title="<div>blah4 blah4</div>"></a>
    </div>
  </div>
</div>

<div onClick="openList();">
  <div class="inc">
    <div class="iWrap">
        <a title="<div>some text 1</div>"></a>
        <a title="<div>some text 2</div>"></a>
        <a title="<div>some text 3</div>"></a>
        <a title="<div>some text 4</div>"></a>
    </div>
  </div>
</div>

My jQuery code

function openList()
{
    var getTitletxt = $(this).find('a').attr("title");
    console.log(getTitletxt);
}

I'm getting undefined in my console.log. Basically i'm trying to fetch text inside title div.

Share Improve this question edited Jun 2, 2015 at 5:48 Matarishvan asked Jun 2, 2015 at 5:44 MatarishvanMatarishvan 2,4423 gold badges42 silver badges71 bronze badges 1
  • 2 first close <a> tag <a title="<div>some text 1</div>"> </a> – Nishit Maheta Commented Jun 2, 2015 at 5:46
Add a ment  | 

3 Answers 3

Reset to default 2

Don't use inline event handlers. You can use on for binding events.

HTML:

<div class="myClass">
<!--       ^^^^^^^^^ -->
    <div class="inc">
        <div class="iWrap"> <a title="<div>blah1 blah1</div>">a</a>
            <a title="<div>blah2 blah2</div>">b</a>
            <a title="<div>blah3 blah3</div>">c</a>
            <a title="<div>blah4 blah4</div>">d</a>
        </div>
    </div>
</div>

<div class="myClass">
<!--     ^^^^^^^^^ -->
    <div class="inc">
        <div class="iWrap"> <a title="<div>some text 1</div>">aa</a>
            <a title="<div>some text 2</div>">bb</a>
            <a title="<div>some text 3</div>">cc</a>
            <a title="<div>some text 4</div>">dd</a>
        </div>
    </div>
</div>

Javascript:

$('.myClass').on('click', 'a', function() {
    alert($(this).attr('title'));
});

Demo: http://jsfiddle/tusharj/zfboe9o1/

Pass current object using this, by default this is not available of you bind handler use javascript inline handler. Your title is also not valid, you problably need on text that is not enclosed in div.

Live Demo

<div onClick="openList(this);">

function openList(obj)
{
    var getTitletxt = $(obj).find('a').attr("title");
    console.log(getTitletxt );
}

Change

<a title="<div>some text 1</div>"</a>

To

<a title="some text 1"> anchor 1 </a>

For custom tooltip class you can make css class, I took it from answer here.

Live demo with tool tip custom style

a.ac[title]:hover:after 
{
}

First close your Tag.

<a title="<div>some text 1</div>"> </a>

Pass this as parameter in function

  <div onClick="openList(this);">

   function openList($this)   
   {
     $($this).find('a').each(function(){  // To get all <a> tag title
        var getTitletxt = $(this).attr("title");
        console.log(getTitletxt );
     }); 
  }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745306226a4621724.html

相关推荐

  • javascript - get this anchor title text jQuery - Stack Overflow

    I'm trying to get all anchor title text inside children div'sMy HTML<div onClick="ope

    6小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信