0

I have this jQuery code as part of a loop to generate divs and set properties for each div.

var names = $('<div/>', {
         id: load1array[j],
    'class': 'bold',
     html: load1array[j]
        });

I want to add a title attribute for each div, but I forgot the syntax. I have tried the codes below but they don't appear to be the right syntax.

var names = $('<div/>', {
         id: load1array[j],
    'class': 'bold',

//attr:('title','text here'),        
//title:'text here' ,    
//'title':'text here' ,  

        html: load1array[j]
        })//.attr( "title", "text here" );

TIA

Edit:

I also saw the answer here Creating a div element in jQuery, which shows that the code that includes the title attribute

jQuery('<div/>', {
    id: 'foo',
    href: 'http://google.com',
    title: 'Become a Googler',
    rel: 'external',
    text: 'Go to Google!'
}).appendTo('#mySelector');

But it did not work as I have tried, following the answer's included links, perhaps the title attribute does not work with <div> tags, but rather <a> tags.

Community
  • 1
  • 1
Jamex
  • 1,164
  • 5
  • 22
  • 34
  • Are you sure 'title':'text here' , didn't work? I'm 99% sure that's correct. – htxryan Aug 29 '13 at 02:01
  • possible duplicate of [jquery create element with attribute differences](http://stackoverflow.com/questions/9898442/jquery-create-element-with-attribute-differences) – Fresheyeball Aug 29 '13 at 02:09
  • 1
    seems to ne fine http://jsfiddle.net/arunpjohny/brFyZ/1/ – Arun P Johny Aug 29 '13 at 02:41
  • 1
    It works fine: http://codepen.io/Fresheyeball/pen/Foayb Your problem is not this syntax. Its something else. – Fresheyeball Aug 29 '13 at 02:32
  • Thanks, the code apparently works with your link for all 3 variations: title:'text here', 'title':'text here', and append at the end .attr( "title", "text here" ). Maybe the problem is with assigning the code to a variable. The easy solution is suggested by ivowiblo, but it is still perplexing. – Jamex Aug 29 '13 at 02:45

1 Answers1

1

Try this:

var names = $('<div/>', {
     id: load1array[j],
'class': 'bold',
 html: load1array[j]
    });

names.attr("title", "text here");
Ivo
  • 8,172
  • 5
  • 27
  • 42
  • Thanks ivowiblo, this code works as expected. But I don't get why it does not work when I chained it to the end of the statement. – Jamex Aug 29 '13 at 02:34