How to add url link from java with css element?

219 Views Asked by At

Here is the code:

<span class='maincaptionsmall'>
Test
</span>

For some reasons of ajax and jquery codes in my page, I can't use HREF as Link, I mean, I can't use something like this following code:

<span class='maincaptionsmall'><a href="http://google.com">Test</a></span>

So, How can i use jquery to get this css element maincaptionsmall and it will set it to href link ?

Even I can't use <div> too, I should use everything as <span> Please give me an example that how to use jquery to set css element as href link.

Thanks in advance

1

There are 1 best solutions below

9
On

For getting .maincaptionsmall this DOM using jQuery use below code,

$('.maincaptionsmall')

and for wrapping innerText you can use wrapInner

$('.maincaptionsmall').wrapInner('<a href="http://google.com"></a>');

Live Demo

Edit

You need to wrap code inside document.ready and also your document is not properly structured.

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(function() // you are missing this
            {
                $('.maincaptionsmall').wrapInner('<a href="http://google.com"></a>');
            });
        </script>
    </head>
    <body>
        <span class='maincaptionsmall'>Test</span>
    </body>
</html>