I cant seem to append to a particular id & class. I want HEllow world to be Appened to only 1st
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
var id =94;
$("#'id',.m").append(" <b>Hello world!</b>");
});
});
</script>
</head>
<body>
<p id="94" class="postitem m" >This is a paragraph.</p>
<p id="94" >This is another paragraph.</p>
<button>Insert content at the end of each p element</button>
</body>
</html>
Two problems:
idvalues must be unique on the page. You’re using the sameidfor two different elements.Your
idvalues are invalid for CSS.idvalues for use with CSS must start with a letter. Since jQuery uses CSS selectors for finding elements, I’d strongly recommend using valid ones. (Those IDs are also invalid for HTML4, but HTML5 allows them. Just not CSS.)If you correct both of those problems, you’ll be fine. Note that if you have unique
ids, you don’t need the “m” class anymore unless you were using it for something else.Live example | source
Separately: I strongly recommend adding a doctype to that HTML. Without one, you’re in quirks mode, and jQuery doesn’t support quirks mode (various calculations will be wrong).