-1
<div id="tile">

<a href="#" title="some other title"><img src="#">
     <div id="newTitle">
     <h1>I want the real title here</h1>
     <p>some useful paragraph</p>
     </div>
</a>

<a href="#" title="some other title"><img src="#">
     <div id="newTitle">
     <h1>Another title here</h1>
     <p>another useful paragraph</p>
     </div>
</a>

</div>

I want to change the anchor titles with the respective DIVs within that anchor using jquery

does anyone have a solution for this?

here's what I've tried:

 <script>
 var eachTile = $("#tile > a"),
 newTitle = eachTile.children("#newTile").text();

 eachTile.attr('title', newTitle);
 </script>

this script shows all texts in one line so all of them has the same text.

2 Answers2

0

You can use the following code :

function changeTitle(){
document.getElementsByName("add a name").title = "Set title";
}

To use with a function callable from event.

CodeIsLife
  • 1,205
  • 8
  • 14
0

Here you can use like this Inside every anchor, "newTitle" should be class, not ID, because you can not call same ID multiple times from multiple items, so use class instead of ID

jQuery

$('#tile a').each(function(){
    var txt = $(this).find('.newTitle').text();
    $(this).attr('title',txt);
});

HTML

<div id="tile">

<a href="#" title="some other title"><img src="#">
     <div class="newTitle">
     <h1>I want the real title here</h1>
     <p>some useful paragraph</p>
     </div>
</a>

<a href="#" title="some other title"><img src="#">
     <div class="newTitle">
     <h1>Another title here</h1>
     <p>another useful paragraph</p>
     </div>
</a>

<a href="#" title="some other title"><img src="#">
     <div class="newTitle">
     <h1>Another title here</h1>
     <p>another useful paragraph</p>
     </div>
</a>

<a href="#" title="some other title"><img src="#">
     <div class="newTitle">
     <h1>Another title here</h1>
     <p>another useful paragraph</p>
     </div>
</a>

</div>
akhan
  • 16
  • 2