0

i have a HTML file index.HTML. i wanna load external files( 1.HTML, 2.HTML, 3.HTML) into a div on button click my HTML is something like this

<div class="buttons">
<button  type="button">button1</button>
<button  type="button">button2</button>
<button  type="button">button3</button>
</div>

<div class="mydiv">

</div>

on click of button1 1.HTML content to be loaded in above div. on click of button2, 2.HTML.. and etc.

i am very new to java script so let me know how to write a script for this. Any help would be appreciated. thanks in advance

user1165077
  • 121
  • 1
  • 3
  • 9
  • possible duplicate of [What are the new frames?](http://stackoverflow.com/questions/9466265/what-are-the-new-frames) – Quentin Apr 09 '13 at 09:24

4 Answers4

1

use load().. and data attirbutes .. try this

html

<div class="buttons">
<button  type="button" data-url="1.html">button1</button>
<button  type="button" data-url="2.html">button2</button>
<button  type="button" data-url="3.html">button3</button>
</div>

jquery

$("button").click(function(){
    $('.mydiv').load($(this).data('url'));
});

NOTE: the selector $('button') selects all the button present in the document.. so its better you give them a class and select it to be specific using class selector .

to be more specific

$('.buttons > button').click(function(){
     $('.mydiv').load($(this).data('url'));
});

OR

<div class="buttons">
<button  type="button" data-url="1.html" class="btnclass">button1</button>
<button  type="button" data-url="2.html" class="btnclass">button2</button>
<button  type="button" data-url="3.html" class="btnclass">button3</button>
</div>

$(".btnclass").click(function(){
    $('.mydiv').load($(this).data('url'));
});
bipen
  • 36,319
  • 9
  • 49
  • 62
0

You may need to use jQuery load()

$('#mydiv').load('test.html');
Adil
  • 146,340
  • 25
  • 209
  • 204
0

you just need to append html after

$('#').append('html content')
lioncin
  • 145
  • 1
  • 1
  • 7
  • Read the question clearly. It is not about adding html content. It is about loading external html file. – arulmr Apr 09 '13 at 09:32
0

try this

 $(document).ready(function(){
    $("button").click(function(){
  var file = $(this).attr('number')+'.html';
  $('.mydiv').load(file);
  })
})
<div class="buttons">
<button  number= '1'  type="button">button1</button>
<button  number= '2'  type="button">button2</button>
<button  number= '3'  type="button" >button3</button>
</div>

<div class="mydiv">

</div>
alwaysLearn
  • 6,882
  • 7
  • 39
  • 67