Question

I'm trying to execute a single function for 3 different div classes within a web page. These 3 div classes contains hyperlinks in each and when these hyperlinks are clicked then the corresponding link should open in another div (here it is accessed with id(div id)).

my code goes here

       $('.div1 a, .div2 a, .div3 a').click(function() {
             var url = $(this).attr('href');
             var height = $('#divid').height();
             var width = $('#divid').width();
                $('#divid').replaceWith( "'<object data="+url+" width="+width+" height="+height+"/>'" );
              return false;
             });

div1, div2, div3 are div classes divid is the id of the div section.

Upto my knowledge the code is correct but unfortunately it doesn't reflects within my site. Is there any wrong with this part.

Was it helpful?

Solution

add a single class for all three div and use this :

$('.singleclass a').on('click',function() {
             var url = $(this).attr('href');
             var height = $('#divid').height();
             var width = $('#divid').width();
                $('#divid').replaceWith( "'<object data="+url+" width="+width+" height="+height+"/>'" );
              return false;
             });

OTHER TIPS

solved the issue.

I added all the three div classes(div1, div2, div3) into one div (gave the class name and the id, then accessed the div with it's id. Eventually, the function applied for all the three classes.

I couldn't know what went wrong previously, but anyhow I solved it. Thank you all for your support.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top