areyouready344
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("keyVal ue"));
Is the syntax for keyValue variable is correct?
Is the syntax for getting the keyValue value is correct?
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("keyVal
Is the syntax for keyValue variable is correct?
Is the syntax for getting the keyValue value is correct?
It looks like you are missing some quotes, right after the "data-keyValue=" and one before the ">"
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(liElemen t);
var liElement = '<li data-keyValue="' + keyValue + '">' + dataValue +'</li>'
$("#list").append(liElemen
it is hard to tell, but on the var liElement line, it goes
single-double-single-singl e-double-s ingle-sing le-single
That makes it so the output is like so:
<li data-keyValue="MyValuefrom keyValueVa riable">My Valuefromd ataValueVa riable</li >
single-double-single-singl
That makes it so the output is like so:
<li data-keyValue="MyValuefrom
ASKER
tried your suggestion strickadd but still says undefined when using
alert($(this).data("keyVal ue"));
alert($(this).data("keyVal
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.
ASKER
how do I test the keyValue for the #list li selection?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks again leakim for getting this working.