3

I can't seem to get this to store. I now that the id and name are definitely valid as I alerted them out right before this code was called:

$('#carText').data("carId", carId);
$('#carText').data("carName", carName);

alert("value stored for carId: " + $("#carText").data("carId").carId);

I get undefined when I try to alert out the value that I supposedly stored.

1
  • 1
    alert("value stored for carId: " + $("#carText").data("carId")); should work Commented Nov 7, 2011 at 9:51

6 Answers 6

2

don't need the .cardId at the end

alert("value stored for carId: " + $("#carText").data("carId"));
Sign up to request clarification or add additional context in comments.

Comments

0

You appear to be using incorrect syntax to retrieve the stored data. Try the following:

alert("value stored for carId: " + $("#carText").data("carId"));

Comments

0

You've stored the data as carId, you don't need that .carId after retrieving it:

alert("value stored for carId: " + $("#carText").data("carId"));

Comments

0

Your alert should look like this: alert("value stored for carId: " + $("#carText").data("carId"));

Comments

0

You don't need to use .carId at the end. It's right.

But some problems with data attribute may causes when you try to get properties which named in camelcase style

Try to rename you data properties to 'carid','carname' It should work

You code may looks like

HTML

<div id="carText" data-cardid="VAZ_1118" data-carname="Lada_Kalina">

Javascript

alert("value stored for carId: " + $("#carText").data("carid"));

Comments

0

data() is based on key:value data, and since all you're storing is a simple value (a string) you don't need to specify .carId at the end. The "route" $(select).data("keyName") is sufficient.

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.