This question already has answers here:
How to compare an html entity with jQuery
(5 answers)
Closed 5 years ago.
I have a span tag as follows:
<span>×</span>
In JavaScript, if I check
$("span").html()
Response is: "-"
Is there any way I can compare
$("span").html() == "×"
instead of $("span").html() == "-"?
You can create an in-memory element and compare it with span
var span = document.querySelector('span');
var dummy = document.createElement('span');
dummy.innerHTML = '×'
console.log(span.innerHTML === dummy.innerHTML)
<span>×</span>
You could decode your html Code Back to normal String.
var decoded = $('<div>').html('×').text();
This value should be comparable.
To treat HTML entities as a string you have to use text() instead of html() method
$("span").text() == "×"
Related
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 7 years ago.
My app read DOM object's attribute value. I want this value to be swapped with some new texts and put back to attribute. The original value is:
"position: absolute; background-image: url(\"http://dealer.raymarine.com/Views/Public/ShowImage.ashx?partno=M81203&view=thumb\")";
but when it should be updated with JS replace method, but nothing is changed, why?
Updating JS code:
var styleValue = ui.helper[0].getAttribute("style");
styleValue.replace("raymarine", "XXX");
styleValue.replace("ShowImage", "ShowImageSystemCreator");
styleValue.replace("view=thumb", "view=png");
ui.helper[0].setAttribute("style", styleValue);
console.log("draggable after text swap: " + styleValue);
You're not saving the value anywhere!
styleValue = styleValue.replace("raymarine", "XXX");
styleValue = styleValue.replace("ShowImage", "ShowImageSystemCreator");
styleValue = styleValue.replace("view=thumb", "view=png");
Make it a one-liner if you want by stringing replaces:
styleValue = styleValue.replace("raymarine", "XXX").replace("ShowImage", "ShowImageSystemCreator").replace("view=thumb", "view=png");
This question already has answers here:
How to get the HTML for a DOM element in javascript
(10 answers)
Closed 9 years ago.
it is impossible to get a string of createElement that you would assign to a variable
var h1 = document.createElement("h1")
h1.innerHTML = "hello world"
alert(h1)
return "[object HTMLHeadingElement]"
when i use appendChild is work but i must use alert or other method
use outerHTML
var h1 = document.createElement("h1")
h1.innerHTML = "hello world"
alert(h1.outerHTML)
Demo: Fiddle
It seems like you want to convert a DOM element to its HTML representation. Put it in a temporary container and access its .innerHTML property:
var div = document.createElement('div');
div.appendChild(h1);
var html = div.innerHTML;
This question already has answers here:
Get array of text values using selector instead of iterating?
(7 answers)
Closed 9 years ago.
I have html with multiple things classed .text. I the perform
console.log($(whatever).find('.foo').text());
The result is a textstring with all the contents in it. Can I save it to an array instead somehow?
You can use the map method to get this done
var textArray = $(whatever).find('.foo').map(function() {
return $(this).text();
}).get();
This will get you an array.. If you want to display it as a string then just use join along with it.
Check Fiddle
var arr = [];
$(whatever).find('.foo').each(function() {
arr.push($(this).text());
});
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: string contains
I want to check if a string contains a specific number in JavaScript, how can I rewrite the statement s contain d in below code?
var s = '11/14/2012';
var d = '14';
if (s contain d) {
//...
}
My case is similar to Check if a string contains a certain number, but how can I implement this action in JavaScript?
Thanks
The easier way is to use index Of.
if(s.indexOf(d) > -1)
var s = '11/14/2012';
var d = '14';
if (s.match(d)) {
alert('Found');
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does jQuery .after() not chain the new element?
This reference code:
$("#id").after(string);
Does a pretty good job inserting the element of the string where its need to.
How can I get a reference to newly inserted HTML element (string)?
var string = '<div id="some_HTML"><span>hello kitty</span></div>';
$jq_elem = $(string); //if it's not a jQuery object, make it one
$("#id").after($jq_elem); //insert into DOM
$jq_elem.css('color', 'red'); //still available
FIDDLE
Try using insertAfter:
var $str = $(string).insertAfter('#id');
This will work if string is HTML.