0

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.

1
  • I would help if you post the generated HTML code. Commented Jun 11, 2012 at 9:53

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

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

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

or

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

Comments

1

This should work

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.