?
Solved

Getting undefined value when using the .data() method for jquery?

Posted on 2011-10-12
9
Medium Priority
?
219 Views
Last Modified: 2012-05-12
Getting undefined value when using the .data() method for jquery?

For example,

dataValue and keyValue are variables.

$("#list").append('<li data-keyValue=" + keyValue + ">' + dataValue +'</li>');

display value, but showing up as undefined.

alert($(this).data("keyValue"));

Is the syntax for keyValue variable is correct?
Is the syntax for getting the keyValue value is correct?
0
Comment
Question by:areyouready344
  • 4
  • 4
9 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 36958269
It looks like you are missing some quotes, right after the "data-keyValue=" and one before the ">"
0
 

Author Comment

by:areyouready344
ID: 36958284
is there a link that shows the correct syntax?
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36958332
Javascript gets a little funky with double and single quotes. It would be easiest if you did something like this:

var liElement = '<li data-keyValue="' + keyValue + '">' + dataValue +'</li>'
$("#list").append(liElement);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 28

Expert Comment

by:strickdd
ID: 36958346
it is hard to tell, but on the var liElement line, it goes

single-double-single-single-double-single-single-single

That makes it so the output is like so:

<li data-keyValue="MyValuefromkeyValueVariable">MyValuefromdataValueVariable</li>
0
 

Author Comment

by:areyouready344
ID: 36958369
tried your suggestion strickadd but still says undefined when using

alert($(this).data("keyValue"));
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36958391
what is the value of the "this" variable? Can you post the code around that? My guess is the "this" variable doesn't actually contain the li element.
0
 

Author Comment

by:areyouready344
ID: 36958406
how do I test the keyValue for the #list li selection?
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36958496
Check this test page : http://jsfiddle.net/nqdgs/


var keyValue = "2011";
var dataValue = "areyouready344";
$("#list").append('<li data-keyValue="' + keyValue + '">' + dataValue +'</li>');
alert($("li:last","#list").attr("data-keyValue"));

// OR?
$('<li data-keyValue="' + keyValue + '">' + dataValue +'</li>').appendTo("#list").data("keyValue","leakim");
alert($("li:last","#list").data("keyValue"));

Open in new window

0
 

Author Comment

by:areyouready344
ID: 36958856
thanks again leakim for getting this working.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question