I'm trying to dynamically add and remove the attribute hidden to some HTML elements with the following code:
<li><a href="#" onclick="SelectImage( 5 ); return false;">6</a></li>
...
<a href="/image/5"><img class="image_to_be_selected" id="image_5" hidden src="/img/5.jpg"></a>
<script type="text/javascript">
$('#image_0').removeAttr( 'hidden' );
function SelectImage( no ) {
$('.image_to_be_selected').prop( 'hidden' );
$('#image_'+no).removeAttr( 'hidden' );
}
</script>
Main idea of this code is to remove the attribute 'hidden' from the first image at the beginning and upon <li>
click hide all images and than to remove the property 'hidden' from the selected image.
But the code is not working properly: $().prop()
doesn't add the attribute as desired. I should use $().attr('hidden',true)
instead, but in this case the HTML will become as:
<a href="/image/5"><img class="image_to_be_selected" id="image_5" hidden='hidden' src="/img/5.jpg"></a>
(with extra ='hidden' chars)
Is it correct from the HTML validity POV? Is there any function in JQuery to add an attribute with no value?