Reputation: 202
so basically I have around 20 paragraphs that are inside a slider, and with every paragraph there's a share button that copies it's innerHTML.
Example:
<p class="parag">content</p>
<button onclick="share();"></button>
<p class="parag">diff content</p>
<button onclick="share();"></button>
<p class="parag">another content</p>
<button onclick="share();"></button>
Can I make something like an array in order to get one paragraph at a time? Because when I press the first share button the code copies the first paragraph and if I press the second share button I still have the first paragraph being copied and not the second one. Hope I explained my issue right and thank you in advance.
Upvotes: 1
Views: 93
Reputation: 2112
you can try like this
<p class="parag">content</p>
<button onclick="share(this);"></button>
<p class="parag">diff content</p><button onclick="share(this);"></button>
<p class="parag">another content</p><button onclick="share(this);"></button>
share function
function share(that){ alert($(that).prev('.parag').html()); }
Upvotes: 0
Reputation: 2412
try this
$(document).ready(function() {
$('button').click(function(e) {
alert($(this).prev(".parag").text());
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="parag">content</p>
<button onclick="share();">content</button>
<p class="parag">diff content</p>
<button onclick="share();">diff</button>
<p class="parag">another content</p>
<button onclick="share();">another</button>
Upvotes: 0
Reputation: 2791
Try this
$('button').click(function(e) {
alert($(this).prev().text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<p class="parag">content</p>
<button>Share</button>
<p class="parag">diff content</p>
<button>Share</button>
<p class="parag">another content</p>
<button>Share</button>
Pure JS:
function share(ele) {
alert(ele.previousElementSibling.innerHTML);
}
<p class="parag">content</p>
<button onclick="share(this);">Share</button>
<p class="parag">diff content</p>
<button onclick="share(this);">Share</button>
<p class="parag">another content</p>
<button onclick="share(this);">Share</button>
Upvotes: 0
Reputation: 5745
try:
$(document).ready(function() {
myArray = [];
$("p").each(function(){
alert($(this).attr("class"))
myArray.push($(this).attr("class"))
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="parag">content</p>
<button onclick="share();"></button>
<p class="parag">diff content</p>
<button onclick="share();"></button>
<p class="parag">another content</p>
<button onclick="share();"></button>
Upvotes: 1
Reputation: 15913
What you can do is
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p class="parag">content</p>
<button>1</button>
<p class="parag">diff content</p>
<button>2</button>
<p class="parag">another content</p>
Javascript
function share(button) {
alert(button.prev().text());
}
$(document).ready(function () {
$('button').click(function () {
share($(this))
});
});
Upvotes: 0
Reputation: 159
function share(element) {
$(element).prev('p').text(); //the p tag content based on the button
}
also mention this
in the funtion
<button onclick="share(this);"></button>
Upvotes: 0