MyBB Community Forums

Full Version: html() to more then one element - the same ID
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

i have little problem
this is only exemple code
<html>

<head>
<script>
var mydiv = $("#mydiv_1");
mydiv.html("Test");
</script>
</head>
<body>
<h1>My Web Page</h1>
<div id="mydiv_1">This is a div element.</div>
<div id="mydiv_1">This is a div element.</div>
<div id="mydiv_2">This is a div element.</div>
<div id="mydiv_1">This is a div element.</div>
<div id="mydiv_2">This is a div element.</div>
</body>
</html>
this code change only first div
what I need to do?
Proper HTML is that each id attribute must be unique on the page. What you can do with jQuery is use a class selector instead since class names can be used repeatedly in the same web page.
Set a class instead id, as @dragonexpert says id is an element on doctype and must be unique.

If you use a class then you got it.

var mydiv1 = $(".div1");

And

<div class="div1></div>

Finally add atributes and events and its done !!!

If you wanna use individual args use this value. Or use ids on divs.
it solve my problem
thanks