Use key as ID to write text to paragraph

Myoji

I am trying to do a each loop and generate the class names to write the text stored in the array dynamically instead of doing them one by one. Nothing seems to write to the paragraphs though?

var user = {};
user['fname'] = 'Hello';
user['lname'] = 'World';
$.each(user, function(key, value) {
      $('p').hasClass(key).text(value);
});
Rocket Hazmat

.hasClass() returns a boolean. It returns whether any of the elements in the collection have that class.

What you want to do is create a selector that searches for that class. Something like this:

$('p').filter('.'+key).text(value);

Or even this:

$('p.'+key).text(value);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related