How To Put FontAwesome In Inner.html - javascript

How am I suppose to put fontawesome icon i.e
`<i class="fa fa-info" aria-hidden="true"></i>
`in a place of "Info" in below inner.html
Below is my javascript
dlg.innerHTML = "<span class='alert-headingg'>Info<" + "/span>";

Try this You have to just put your icon element inside your span.It will work for you.
dlg.innerHTML = "<span class='alert-headingg'><i class="fa fa-info" aria-hidden="true"></i></span>";

<i class="icon-github"></i>
div.github:before
{
content: "\f09b";
font-family: FontAwesome;
}

Related

Text and font-awesome icon wont change on toggle

I am trying to change the text of a button when clicked and back to normal when clicked again. The button is using a font awesome icon so figured I would try using this.html instead of this.text, but for some reason it is not recognising the HTML I am indicating?
If I change the HTML on the else statement then it will change on click which is what makes me think it is an issue with it not recognizing my HTML.
I know it might be something really stupid but cant seem to figure it out.
$(document).ready(function(){
$("#submitbutton").on("click", function(){
if($(this).html()=="View code <i class=\"fas fa-chevron-right fa-xs\"></i>")
{
$(this).html("Close code <i class=\"fas fa-chevron-right fa-xs\"></i>");
} else {
$(this).html("View code <i class=\"fas fa-chevron-right fa-xs\"></i>");
}
})
});
<div class="getcodebuttoncontainer">
<button
type="button"
class="btn-primary"
id="submitbutton"
onclick="retrieveData()"
>View code <i class="fas fa-chevron-right fa-xs"></i></button>
</div>
It seems as if the only change is the text ( "View || Close ") so you can simply put that variable text in spans - one with a display: none styling and then on the click - toggle the visibility of each.
$(document).ready(function(){
$("#submitbutton").on("click", function(){
$(this).find('span').toggle();
})
});
#submitbutton span:nth-of-type(2) {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="getcodebuttoncontainer">
<button
type="button"
class="btn-primary"
id="submitbutton"
>
<span>View</span>
<span>Close</span>
code <i class="fas fa-chevron-right fa-xs"></i>
</button>
</div>

add the icon tag to the innerHTML in a button

I'm trying to create a function in JavaScript, which on the button click changes class and adds a green checkmark, which is the icon tag in html.
I tried this:
function markerValg(btnID) {
knap = document.getElementById(btnID);
knap.classList.remove("unselected");
knap.classList.add("selected");
document.getElementById(btnID).innerHTML = += <i class="fa fa-check ikon"></i>
Change the last line of code to
document.getElementById('test').innerHTML = '<i class="fa fa-check ikon"></i>';
<button id='test'>Check</button>

How to extract text from element with attribute style="display:none;

HTML of that part is:
<div class="review-small-text">
<span class="stars-rate">
<span property="starsRating">
<i class="fa fa-star-yellow fa-star"></i>
<i class="fa fa-star-yellow fa-star"></i>
<i class="fa fa-star-yellow fa-star"></i>
<i class="fa fa-star-yellow fa-star"></i>
<i class="fa fa-star-yellow fa-star"></i>
</span>
</span>
<span property="reviewRating" typeof="Rating" style="display:none;">
<span property="ratingValue">5</span>
<span property="bestRating">5</span>
<span property="worstRating">0</span>
</span>
<span property="itemReviewed" typeof="Service" class="">Liposuction</span> </div>
I'm trying to extract the second span's ratingValue of a particular review using selenium and i tried to extract that value by using this css selector:
'div.review-small-text>span:nth-of-type(2)>span:nth-of-type(1)'
but it is giving me an empty string.
have also tried this one
'div.review-small-text>span:nth-child(2)>span:nth-child(1)'
so I think the problem is not in the css-selector. Display none is creating an issue here.
Is there any possible way to extract that value?
Python source code that i have tried so far is:
from selenium import webdriver
import time
url = "myurlhere"
driver = webdriver.Chrome()
driver.get(url)
time.sleep(3)
all_reviews_listings = driver.find_elements_by_xpath("//div[#id='tab_reviews']/div[#class='provider_all_Reviews']/div[#id='pnlReviews']/div")
for review in all_reviews_listings:
review_rating = review.find_element_by_css_selector('div.review-small-text>span:nth-of-type(2)>span:nth-of-type(1)').text
print("Review Rating: ", review_rating)
Here is the css to get the ratingValue.
Using JavaScript:
review_rating = driver.execute_script("""return document.querySelector(".review-small-text > span[property='reviewRating'] > span[property='ratingValue']").textContent""")
Without JavaScript: Alternatively you can also do this.
driver.find_element_by_css_selector(".review-small-text > span:nth-child(2) > span[property='ratingValue']").get_attribute("textContent")
The ancestor tag is having the attribute style="display:none;, so to extract all the reviewRatings you can use the following solution:
driver.execute_script("arguments[0].removeAttribute('style')", driver.find_element_by_css_selector("div.review-small-text span[property='reviewRating'][typeof='Rating']"))
print([element.text for element in driver.find_elements_css_selector("div.review-small-text span[property='reviewRating'][typeof='Rating'] span")])

JavaScript convert string into HTML element

I have javascript string with HTML elements. I want to display these elements on page but not in string form.
var stars = a;
if (stars == 5){
return '<i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i>'
}
So how it is possible to convert this string?
Use the innerHTML property of an element.
E.g.
let element = document.getElementById('myElement');
element.innerHTML = "<i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i>"
Use the innerHTML property to add the string as elements to the document. And you have to use the correct quotes in the string. Your string is not valid.
var stars = 5;
if (stars == 5) {
document.getElementById('body').innerHTML += '<i class="fas fa-star">a</i><i class="fas fa-star">a</i><i class="fas fa-star">a</i><i class="fas fa-star">a</i><i class="fas fa-star">a</i>'
}
.fas
{
background-color:green
}
<body id="body"></body>
you can use insertAdjacentHTML
const divInString = '<div class="todo">Stuff</div>'
const parentOfDiv = document.querySelector(".parent")
parentOfDiv.insertAdjacentHTML("beforeEnd",divInString)
I was looking for a solution for this problem as well and this is what solved my issue.

Using a font awesome icon and text in a Datatables column header

There is probably an easy answer here but I am trying to put both text and a font awesome icon in the header of a datatable I am trying to update (should go after the the title - 1st line of code). So far I am just getting a blank or a square. Here's a block of code from the map. Anyone have any ideas? Does this have to be done in the CSS instead? Thanks
{title:'enabled' + '<i class="fa fa check green"></i>', class:'enabled small-screen small-screen-2-col', data:function(row, type, val, meta) {
var isEnabled = row.enabled || 0;
if (type=='display') {
return (isEnabled) ? '<i class="fa fa-check green"></i>' : '' ;
}
return isEnabled;
},
The first classname is wrong. It should be <i class="fa fa-check green"> instead of <i class="fa fa check green></i>. You forgot the dash between fa and check

Categories

Resources