3

I'm getting a javascript object via ajax. I need to attach this object to a div in order to be recovered later, for example, on a click event.

If instead of an object I had a variable I would push it into the html tags like this:

'<div variable="'+value+'"></div>';

And I would recover its value like this:

var value= $(this).attr('variable')

Could you suggest me a good approach to do that with objects?

Thank you very much!

2
  • Is it really required to attach it to the html div? Can't you store in variable or use localstorage? Commented Mar 23, 2014 at 12:34
  • Why don't you just store it to a global variable for later? Will you not know which variable to retrieve from later? Is the json object related directly to the div you are attaching it to? This is really an odd approach without more details. Commented Mar 23, 2014 at 12:50

4 Answers 4

5

The easiest way is to do this:

<div id="myDiv">...</div>

In javascript

var myDiv = document.getElmentById('myDiv');
myDiv._variable = variable;

You can recover this later if you want, simply using the same myDiv variable, or, again, with document.getElementById() or any other DOM method that returns the element.

var variable = myDiv._variable;

The downside of doing it this way is that you can't specify, in the server, or from the markup, which object you want to attach to the element.

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

4 Comments

I think this is the best approach if the json object needs to be kept with the element in question. The name of the property that the JSON is stored could be something that signifies the object so that more than one can be attached, like myDiv.objectA = variable;
Of course, I'd use a property name with meaning. I used _variable in the example because I don't know what exactly @marc-vilalta wants to store.
right on. I just wanted it to be clear that _variable didn't have special meaning and also that your downside isn't really such a downside since the OP's question doesn't indicate that the object reflects where it's going to go no matter what solution is used.
typo getElmentById => getElementById
2

If use JQuery you could use the data storrage functionallity

See data documentation of JQuery

//To store value or obj:
$("#myDivId").data("myKey", valueVar);
//Later to load:
var fetchValue = $("#myDivId").data("myKey");

Comments

0

Using a template engine would be the best approach, dividing logic from view, and that way you can parse full object properties to an html.

in this example i use jQuery & UnderscoreJs template engine.

javascript part:

 $(document).ready(function(){
     var user = { name:"Ofer", age:"29" }

     var markup = _.template($("#userTemplate").html(), user);
     $('#userContainer').html(markup);
});

html part(goes inside body)

<div id="userContainer">
</div>

<script id="userTemplate" type="text/template">
<fieldset>
 <legend>User</legend>
 Name: <%= name %><br>
 Age: <%= age %> <br>
 </fieldset>
</script>

The function _.template($("#userTemplate").html(), user); can be called from within the complete callback function of the ajax, passing the data from the results to the function (user variable would be the data)

Comments

0

Using https://api.jquery.com/prop/

yourObj = [];

//now fill your object with your data

$row = $("td.yourClass")

$row.prop("dataObj", yourObj );

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.