Geeth
Geeth

Reputation: 5343

How to access the elements in <script> inside the body tag

I am using <script> inside the body tag.

<script type="text/javascript" language="javascript">
 $('#audioVolume').text($('#audioVolume').text(Math.round((document.mediaPlayer.Volume + 2000) / 10) + "%"));
</script>

Error: Microsoft JScript runtime error: 'undefined' is null or not an object.

Need: I want to access the html elements in <script inside the body tag.

Upvotes: 2

Views: 297

Answers (2)

tvanfosson
tvanfosson

Reputation: 532435

Best guess - document.mediaPlayer is undefined. I think you've also got a typo - you're setting the text of the element to the result of setting the text of the element? Try wrapping the whole thing inside a ready function so that it doesn't run until the DOM is loaded. If that doesn't work, then fire up the debugger tools in IE8 and see which element isn't defined.

<script type="text/javascript" language="javascript">
 $(function() {
     $('#audioVolume').text(Math.round((document.mediaPlayer.Volume + 2000) / 10) + "%"));
 });
</script>

Upvotes: 4

Dogbert
Dogbert

Reputation: 222128

<script type="text/javascript" language="javascript">
$(document).ready(function(){
 $('#audioVolume').text(Math.round((document.mediaPlayer.Volume + 2000) / 10) + "%");
});
</script>

Upvotes: 0

Related Questions