0

I am trying to implement something like this. IF you click the button it would get the HTML of the above elements and insert them in an alert :

<div id="0001">

    <h5 class="title">Hello World </h5>
    <h4 class="date">2014-07-19 </h4>

    <button onclick="addCalendar('#0001. #title','#0001. #date')"> Add to Calendar </button>
</div>    

<div id="0002">

    <h5 class="title">Bye Bye</h5>
    <h4 class="date">2014-07-22 </h4>

    <button onclick="addCalendar('#0002. #title','#0002. #date')"> Add to Calendar </button>
</div>

<script>
function addCalendar(title,date){
    alert(title + ": " + date);   
}
</script>
7
  • what do you mean by getting HTML full like this <h5 id="title">Hello World </h5> Commented Jul 19, 2014 at 9:05
  • note: id should be unique Commented Jul 19, 2014 at 9:11
  • @Grundy yes of course Commented Jul 19, 2014 at 9:12
  • in your sample you have two elements with same id :-) Commented Jul 19, 2014 at 9:13
  • 2
    An ID should be unique for the page. If you need to repeat a selector, the convention is to use a class. Commented Jul 19, 2014 at 9:14

1 Answer 1

1

This code should works..

If you want to get the value of that HTML:

$("button").click(function(){
    var value = $(this).siblings("#title").text();
    value += $(this).siblings("#date").text();
    alert(value);
});

If you want to get the HTML tags too:

$("button").click(function(){
    var value = $(this).siblings("#title").html();
    value += $(this).siblings("#date").html();
    alert(value);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.