Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Jquery - loading a external content

    I want to write one script to grab a page with the same name as the id of the div tag.

    ex

    <a href="javascript:void(0);" id="vid1"><img src="barge/1.jpg" class="vidspace"> </a>
    <a href="javascript:void(0);" id="vid2"><img src="barge/2.jpg" class="vidspace"> </a>

    And I want it to be like

    ('a').click function (this.id)
    load.(file/ + this.id)

    I'm really tired and can't do the right code, but I think you get the idea.

    Thanks.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    something like this:

    Code:
    $('a').click(function(e){
    e.preventDefault();
    var myid = $(this).attr('id')
    $("#mydiv").load('someurl.com/pages/'+myid+'.html')
    });
    and remove that javascript:void nonsense. you are using jquery you dont need to do that anymore.

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,696
    Thanks
    6
    Thanked 1,011 Times in 984 Posts
    Quote Originally Posted by DanInMa View Post
    you are using jquery you dont need to do that anymore.
    You don’t/didn’t even have to do that with plain JavaScript if you write unobtrusive code.

    And speaking of unobtrusive JavaScript, here is the perfect example, tailored to your specific problem:

    Code:
    <a href="pageID.html">…</a>
    …
    …
    <div id="result"></div>
    PHP Code:
    $('a').click(function(e) {
      $(
    '#result').load($(this).attr('href'));
      
    e.preventDefault();
    }); 
    No redundant ID or JavaScript required in the HTML and the contents are accessible in any case (e. g. even if JS is not available for some reason).
    Last edited by VIPStephan; 08-18-2013 at 01:47 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •