I am trying to store a DOM element into a variable by using this code:
var save_status = $(this).siblings(".save-status")[0];
save_status.append("<img src="images/loading-small.gif" width=30 />");
I get the following error though:
Uncaught SyntaxError: Unexpected identifier
Any idea why I might be getting this error? Thanks!
I am trying to store a DOM element into a variable by using this code:
var save_status = $(this).siblings(".save-status")[0];
save_status.append("<img src="images/loading-small.gif" width=30 />");
I get the following error though:
Uncaught SyntaxError: Unexpected identifier
Any idea why I might be getting this error? Thanks!
Share Improve this question asked Jul 19, 2011 at 0:26 egidraegidra 3472 gold badges9 silver badges18 bronze badges3 Answers
Reset to default 10You're getting the error because your quotes are not escaped (or literal).
To answer your other question (per the title and example), you can save a jQuery object, or a DOM element, but they're not the same. For example:
var x = $(selector); // returns jQuery object
var y = $(selector)[0]; // returns DOM element
x.append('stuff'); // use jQuery functions directly
$(y).append('stuff'); // pass the DOM element to jQuery first
Replace
save_status.append("<img src="images/loading-small.gif" width=30 />");
with
save_status.append('<img src="images/loading-small.gif" width=30 />');
The problem is in your quotations.
I see several things. Try this example:
<style>
#mydiv{
display: block;
width: 100px;
height: 100px;
background-color: red;
}
</style>
<script src="../assets/js/jquery-1.6.min.js" type="text/javascript"></script>
<script>
$(function(){
$("#mydiv").click(function(){
var save_status = $(this).siblings(".save-status").next();
save_status.append('<img src="images/loading-small.gif" width=30 />');
alert(save_status);
});
});
</script>
<body>
<div id="mydiv">
<div></div>
<div></div>
<div class="save-status"></div>
</div>
</body>
The differences are the quotes and use ".next()"