4

I am creating a dynamic div in html which consists of multiple checkboxes. All these checkboxes and divs are being dynamically added to the html. I need to store some data about each div in the html to be accessed by javascript later. Can anyone show me an example where data can be added and retrieved dynamically in a div? I know HTML5 allows it and there are some other hacks to do it, but I am having trouble with syntax I guess.

2
  • 3
    You may want to look into data attributes. Commented Mar 9, 2014 at 17:02
  • 1
    It really depends on how you want to go about it. You can always store stuff in data attributes like @Whymarrh said, or regular non-standard attributes if you don't care about compliance, but you could also do PHP/ASP/CGI if you want stuff from server-side, or just plain JavaScript/jQuery if all the data will be changed on the client side and no new data will come from the server. You could even do it from AJAX. Commented Mar 9, 2014 at 17:10

5 Answers 5

2

Try to do it using JavaScript:

SomeClass.someVariable = document.getElementById('divid');

Otherwise if you mean to access custom data that has to used as attribute in your HTML tags then use

data-XXX = 'YYY';

And access it with JS:

document.getElementById('divid').dataset.XXX;
Sign up to request clarification or add additional context in comments.

Comments

2

This post explains data-* attributes.

You can create custom attributes within your divs like this:

<div id="div1" data-text="Hello. This is a custom attribute."></div>

Notice the data- prefix. this is absolutely necessary. Then (using jQuery) you can access the custom attribute:

$('#div1').data('text'); => "Hello. This is a custom attribute."

So using this you can do stuff like:

if($('#div1').data('text') != "FreddieBobman"){
    alert("HI!");
} else {
    alert("Forever Alone!");
}

The above example will alert "HI!" because $('#div1').data('text') does not contain FreddieBobman, it is in fact "Hello. This is a custom attribute."

To create these attributes use the following:

$('#div1').attr('data-name', 'value');

Our div with id of div1 now has another attribute, data-name, and the attribute has a value of value. Of course, you can change the value of attributes as well:

<div id="div1" data-text="Hello. This is a custom attribute."></div>
<script src="jquery.js"></script>
<script>
    (function(){
        $('#div1').attr('data-text', 'This is cool.');
    }());
</script>

Now the div has data-text equal to "This is cool.", not "Hello. This is a custom attribute."

Comments

0

It is Obtain by the data attributes that you add dynamically to the elements ,And retrieve when you want !

SET Attribute :

        var div = document.createElement('div');
        div.setAttribute('data','my_data');
        div.innerHTML = "I am a div";
        document.body.appendChild(div);

GET Attribute :

        div.getAttribute('data')

WORKING DEMO

Comments

0

you can use attributes for your html tags. Also, in html5 you can also use custom attributes

1 Comment

Why not put your references in your post?
0

What you want is to add or retrieve data from div tags dynamically?

Using javascript function you can simply get corresponding div element using its id,

var container = document.getElementById('your_element_id');

Then you can add what you want.

var stringToAdd = "<p>something you want to add</p>";
container.innerHTML = stringToAdd;

Also you can use a different class with different features and set it as your div class. you need to add your features inside style tag under your class name. Then,

var elementID = document.getElementById(ID);
elementID.className ="your_new_class_name";

or you can set attributes for your tag.

elementID.setAttribute('display','inline');

Using jquery with javascript,

var elementID = document.getElementById(ID);
$(elementID).replaceWith( "<p>something you want to replace</p>" ); 

using class name or id you can dynamically append content using,

var stringToAdd = "<p>something you want to add</p>";
$(".your_class_name").append(stringToAdd);

also you can remove whole element using,

$(elementID).remove();

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.