Is it possible to add to a div with a given attribute?

How do you add to a specific div with the specified attribute?

ex.

<div attribute="234"></div>

$('#divwithattribute234').append('test');
+3
source share
3 answers

I highly recommend reading this post about using custom attributes because it will not be valid even in HTML5, which will allow custom attributes. Why not just use the class, as you can have as much as you want?

<div class"some_class some_other_class target_class"></div>

Of all the above classes, assuming that "target_class" is used for identification <div>, you should select it and add to it using

$(".target_class").html('test');

Update:

<div>, , ( ^=, " " ) , <div>. , div ...

HTML

<a href="#" class="some_class" id="target_div_1">Add to DIV 1</a><br />
<a href="#" class="some_class" id="target_div_2">Add to DIV 2</a><br />
<a href="#" class="some_class" id="target_div_3">Add to DIV 3</a><br />

<div class="some_other_class target_1">Div 1</div>
<div class="some_other_class target_1">Div 2</div>
<div class="some_other_class target_1">Div 3</div>

JQuery

$("a[id^=target_div_]").click(function(event) {
    var targetID = $(this).attr('id').substr(11);
    $("div.target_" + targetID).html('content got inserted in div ' + targetID);

    event.preventDefault();
});

JSFiddle.

, !

+2
jQuery('div[attribute=234]').html('test');

jsfiddle

+4

This is how you will match your div ( <div attribute="234"></div>):

$("div[attribute=234]").html('Lets write something...');
+1
source

Source: https://habr.com/ru/post/1765021/


All Articles