0

I have made a div solely for holding default data/text so I can retrieve it with jquery, and avoid re-stating text messages etc. in jquery too. It works, and lets me have ONE place to edit text, but maybe its too messy, I'm not sure..

Like so (html):

<div id="defaultdata" data-data='{"yestext":"Yes please, thank you!", "yesclass":"yesstyle", "notext":"No thank you..", "noclass":"nostyle"}'></div>

Then in jquery I have:

if (someclass == 'yes') {

someelement.text($('#defaultdata').data("data").yestext);
someelement.removeClass($('#defaultdata').data("data").noclass);
someelement.addClass($('#defaultdata').data("data").yesclass);

} else {

someelement.text($('#defaultdata').data("data").notext);
someelement.removeClass($('#defaultdata').data("data").yesclass);
someelement.addClass($('#defaultdata').data("data").noclass);

}

My question is.. wouldn't it be better to retrieve the data("data") element once (as json maybe?) and then refer to that instead of having so many data("data").xxxx requests?

I mean.. Doesn't jquery scan, select and parse it each time I refer to data("data"), or is it already set in an array/object once used?

4
  • Right.. so why don't you set a variable to $('#defaultdata').data("data") so you don't have to keep having jQuery spend cycles on re-finding and interpreting the data? Commented Jul 27, 2014 at 15:36
  • Related: stackoverflow.com/questions/291841/… (the answer is no) Commented Jul 27, 2014 at 15:37
  • see code.jquery.com/jquery-2.1.1.js it will answer all your questions on jquery Commented Jul 27, 2014 at 15:37
  • @Skram yes thats what im thinking of now (writing the issue helps clear the mind some ;)). But how would I do that? Just var somevar = $('#defaultdata').data("data"), and then how do I retrieve from that? Commented Jul 27, 2014 at 15:37

3 Answers 3

1

Combining charlietfl and Will's points, you could so something like this:

var someelement = $("#someelement");
var defaultData = $('#defaultdata').data('data');

if (someclass == 'yes') {
    someelement.text(defaultData.yestext).removeClass(defaultData.noclass).addClass(defaultData.yesclass);

} else {
    someelement.text(defaultData.notext).removeClass(defaultData.yesclass).addClass(defaultData.noclass);

}

Here's a jsfiddle: http://jsfiddle.net/tkSyd/1/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I have combined (chained) the someelement.xxx selector calls too. My code is much leaner now ;)
1

In jquery, if you don't refer it to a variable, then it will run through whole DOM to find your selector every time. So in your case, $('#defaultdata').data("data") will run many times, which definitely will reduce the efficiency, so I recommend you to refer it to the variable.

2 Comments

Yes, makes sense.. How do I select from that var, once it's set?
not sure about what you mean? like this : var that = $('#defaultdata').data("data")
1

You can retrieve the whole data object once simply using :

 var data = $('#defaultdata').data();

then you can access various properties using:

var yestext= data.data.yestext

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.