views:

62

answers:

3

Hey guys quick question,

How do you append to a specific div with a specified attribute?

ex.

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

$('#divwithattribute234').append('test');
+1  A: 

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

$("div[attribute=234]").html('Lets write something...');
Garis Suero
thanks garis. appreciate it.
Scarface
+4  A: 
jQuery('div[attribute=234]').html('test');

Check on jsfiddle

NAVEED
you are both right but I like the interactive example, thanks naveed.
Scarface
You are welcome.
NAVEED
+2  A: 

I strongly recommend reading this post about using custom attributes because that wouldn't be valid even in HTML5 which will allow custom attributes. Why not simply use a class since you can have as many as you want ?

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

Of all the above classes, assuming 'target_class' is the one used for identifying the <div>, you would select it and append to it with

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

Update:

If you have more than one target <div>s and you're looking for a specific one, use a wildcard selector on the trigger (in our case we'll use the ^= operator which means 'starts with') and then assign its ID to a variable which we then pass to the <div> selector. Say you want to add your text to a div when a link is clicked ...

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();
});

See it on JSFiddle.

Hope this helps !

FreekOne
sure you can set as many classes as you want, but the reason I use a custom attribute, is so multiple values can be taken from a div if you have class=1 class=2 how do u find a specific value?
Scarface
You can always use a wildcard selector ;) Please see my updated answer, I've also included a JSFiddle live example.
FreekOne
I really appreciate your answer, please leave the first portion of your answer intact so I can use for future reference. However, I was thinking more in the terms of value holding such as <div value="2" id="4"></div>. In another example, what if you wanted to use a div and jquery ajax script to send information to a php script, instead of using a form, you use the value holding div and access its attributes. Is there a substitution for that as well?
Scarface
Glad to be of help. The approach that you're proposing is flawed to begin with since `<div>` elements are really not meant to hold values that way. If you insist on placing values inside `<div>`s, give them a `display:none;` attribute and at least do it like `<div id="id_4">2</div>` and retrieve that value with something like `$("div#id_4").html();`. But then again, why don't you use hidden form elements instead since this is what they're meant for ? Something like `<input type="hidden" name="id_4" value="2">`.
FreekOne
i guess your right, but I was thinking in terms of optimizing and reducing code, so that is why I thought div values was cleaner
Scarface
To be honest, in terms of "cleaner" I would personally rather see properly indented code more than anything else. As for optimization, whatever you would win in HTML syntax (although as you probably figured out by now, I wouldn't call invalid syntax a "win"), you would lose in jQuery when writing static triggers for each and every div that you use as opposed to one that uses dynamic selectors. Either way, I'm glad I was able to convince you that good syntax is good and thank you for accepting my answer. :)
FreekOne