0

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.

6
  • try putting your code in between $(document).ready(function(){ //code here });
    – Adjit
    Apr 22, 2014 at 19:25
  • Make sure you're including the jQuery library in your site's pages. Are there any errors in your browser's console? Apr 22, 2014 at 19:26
  • 1
    Perhaps you need to add id="divid" to the <object> you're replacing it with. Apr 22, 2014 at 19:29
  • jquery library is included, no errors in browser. I couldn't spot where its going wrong
    – Giri
    Apr 22, 2014 at 23:13
  • Blazemonger i think soo there wouldn't be any difference adding id="divid"..
    – Giri
    Apr 22, 2014 at 23:15

2 Answers 2

1

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;
             });
2
  • Doesn't change anything. Just a different way to do the same thing
    – Adjit
    Apr 22, 2014 at 19:27
  • While this is useful it really doesn't answer the OP's question about the code not working in his site. Apr 22, 2014 at 19:27
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.