I need your help please. In getting something work, I've tried pulling up information from feeds and blog, yet it's not trusted. Here is my code...
<div class="select_obj">
<h4>Promote Channel</h4>
</div>
<div class="select_obj">
<h4>Increase Conversion on Site</h4>
</div>
<div class="select_obj">
<h4>Increase Conversion on App</h4>
</div>
...
<div class="select_obj">
<h4>Get Video View</h4>
</div>
How To select and Get the text within the clicked on h4. I Just don't know how to start, Please I need y'all help. Thanks.
Well you can have 2 div's with the same name provided one is an “div id” and the other is a “div class”. But you can't have the same name for two “divs” or “classes”.
To select all text inside an element such as DIV, we can simply use JavaScript document. createRange() method to create a range, and than using range. selectNodeContents() we can set node range (start and end), after which use selection. addRange() to select the range of the element.
Any sort of content can be put inside the <div> tag!
Using javascript
function getTxt(x){
alert(x.innerHTML);
}
<div class="select_obj">
<h4 onclick="getTxt(this);">Promote Channel</h4>
</div>
<div class="select_obj">
<h4 onclick="getTxt(this);">Increase Conversion on Site</h4>
</div>
<div class="select_obj">
<h4 onclick="getTxt(this);">Increase Conversion on App</h4>
</div>
...
<div class="select_obj">
<h4 onclick="getTxt(this);">Get Video View</h4>
</div>
A simple solution using jquery
$(".select_obj").click(function(){
//to get the content
var txt=$(this).children("h4").text();
alert(txt);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="select_obj">
<h4>Promote Channel</h4>
</div>
<div class="select_obj">
<h4>Increase Conversion on Site</h4>
</div>
<div class="select_obj">
<h4>Increase Conversion on App</h4>
</div>
...
<div class="select_obj">
<h4>Get Video View</h4>
</div>
Using jQuery you can do like this
$('h4').on('click',function(event){
alert($(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="select_obj">
<h4>Promote Channel</h4>
</div>
<div class="select_obj">
<h4>Increase Conversion on Site</h4>
</div>
<div class="select_obj">
<h4>Increase Conversion on App</h4>
</div>
<div class="select_obj">
<h4>Get Video View</h4>
</div>
Using native javascript
var getAllH4 = document.querySelectorAll('h4');
getAllH4.forEach(function(item, index) {
(function(item, i) { // creating closure
item.addEventListener('click', function() {
alert(item.textContent);
});
}(item, index))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="select_obj">
<h4>Promote Channel</h4>
</div>
<div class="select_obj">
<h4>Increase Conversion on Site</h4>
</div>
<div class="select_obj">
<h4>Increase Conversion on App</h4>
</div>
<div class="select_obj">
<h4>Get Video View</h4>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With