rakop rakop - 7 months ago 24
Javascript Question

writing javascript in html page

I found JavaScript I like to use but I need It to be written in the html page and not as a separate file ... Here is a JS fiddle

Here is how I tried to write it:



<head>
<style>
p span {color:blue;}
</style>
</head>

<body>

<p>sony sensor 2.1mp</p>

<script>
$('p').html(function(index, value) {
return value.replace(/(\d+)/g, '<span>$1</span>');
});
</script>

</body>





I know that I can wrap the numbers with
<span>
tag but
in my actual page I have numbers dozens of times during the text and this is way I rather use the script...

What do I need to change in the script to make it work?

Answer

Two things, use <script type='text/javascript'></script>. And add a reference to jQuery library.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

and put your scripts inside $(document).ready().

Comments