Link to home
Start Free TrialLog in
Avatar of areyouready344
areyouready344Flag for United States of America

asked on

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

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?
Avatar of strickdd
strickdd
Flag of United States of America image

It looks like you are missing some quotes, right after the "data-keyValue=" and one before the ">"
Avatar of areyouready344

ASKER

is there a link that shows the correct syntax?
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);
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>
tried your suggestion strickadd but still says undefined when using

alert($(this).data("keyValue"));
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.
how do I test the keyValue for the #list li selection?
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks again leakim for getting this working.