Sana Joseph
Sana Joseph

Reputation: 1948

Get Attribute of Dynamic string

I have this container:

  <Div id="ListContainer">

I append these data to it :

 ' <a class="lesson" subjectID="'+sbj_ID+'"><b>
  <span class="lesson_subject">' + sbj_Name + '</span></b></a> ';

I want to put the value of sbj_ID & sbj_Name in a variable.

    localStorage['SubjectID']= "value of sbj_ID";
    localStorage['SubjectName']="value of sbj_Name";

But I can't access them.

I tried :

     $('#ListContainer').find('.lesson').attr('subjectID')
     $('#ListContainer .lesson').children[0].getAttribute('SubjectID')
     $('#ListContainer .lesson').children[2].innerHTML;

But they didn't work.

Upvotes: 0

Views: 215

Answers (3)

slash197
slash197

Reputation: 9034

This should work

localStorage['SubjectID'] = $('.lesson').attr('subjectID');
localStorage['SubjectName'] = $('.lesson_subject').text();

Upvotes: 1

Curtis
Curtis

Reputation: 103348

I don't know how you are appending the string, but the following is a working example:

var sbj_ID = 3;
var sbj_Name = "test";

var str = '<a class="lesson" subjectID="'+sbj_ID+'"><b><span class="lesson_subject">' + sbj_Name + '</span></b></a>';

$("#ListContainer").append(str);

alert($('#ListContainer').find('.lesson').attr('subjectID'));

-- See Demo --

I'm also not sure how LocalStorage is being used, but you should set the variables as the value, not the strings you have used:

localStorage['SubjectID']= sbj_ID;
localStorage['SubjectName']= sbj_Name;

Upvotes: 2

Adil
Adil

Reputation: 148110

Try this

localStorage['SubjectID']= $('.lesson').attr('subjectID');

localStorage['SubjectName']=$('.lesson_subject').text();

or

 localStorage['SubjectName']=$('.lesson_subject').html();

Upvotes: 1

Related Questions