I am trying to use javascript to grab the innerHTML of an element. The innerHTML is the results of a query ran. Sometimes the query returns no data so there is nothing inside of the div. I do an alert and it says '[object HTMLDivElement]'. I need to account for this when there is no data for the innerHTML in an if statement. Does anyone know how I would do this? How would I write the IF part?
var HSA_EmployeeCont = document.getElementById('HSA_EmployeeCont').innerHTML;
//alert(HSA_EmployeeCont);
if (HSA_EmployeeCont == 'object HTMLDivElement') {
...
Check the length of the innerHTML:
if (!HSA_EmployeeCont.length)
console.log("Nothing here!");
if (!HSA_EmployeeCont) should do the trick.
Your alert should be showing nothing, perhaps you forgot to add innerHTML before you tested with an alert?
Related
I've loaded to Cheerio a product from belk.com.
I can get to the price element using the selector [class="price"], but when I try to get its text using text(), I get an empty string.
If I do $('class="price"').contents() I can see a child with the text, but it seems like the wrong way to go. Is there a generic method to getting the element text?
Thanks!
That HTML has both p and span tags with class price, so you'll need to be more specific. This works for me $('p.price span.price').text()
Apparently, this is expected node REPL behavior.
The values tested negative for null and in debugger mode I could see the scraped value from Cheerio. Because it contained \r characters, printing it to console looked as if it's an empty string.
i am using this:
$newPostID_fromCookie = $_COOKIE['scrollToBottom']+5000;
echo "
<script>
window.onload = function () {
//document.getElementById('$newPostID_fromCookie').scrollIntoView();
alert('$newPostID_fromCookie');
}
</script>
";
This correctly shows the value from the cookie in the alert, but i am getting 'is null' error when trying to use the value in the getElementById.
How can i use the value in this?
Your $newPostID_fromCookie seems to be an integer. In xHTML, this is not valid and maybe your browser does not like it. You should use a string (with a prefix for example):
document.getElementById('cookie_$newPostID_fromCookie').scrollIntoView();
Of course, change your HTML in consequence.
The getElementById() does a DOM lookup and returns the first element with the specified id.
In this case $newPostID_fromCookie should be an id of an existing element in the DOM.
How do I use the JavaScript console to see why this code:
// Empty info
if ($('.perma-info').text() == '') {
$('.perma-info').remove();
}
Doesn't work in this page: http://dev-indiehaz.tumblr.com/post/22897976111/vans-vw
I want it so that if the element is empty, I can remove it.
You could start by:
console.log($('.perma-info'));
and then you observe the console. 2 possibilities: you get an empty resultset in which case you obviously should check your selector as there probably isn't an element with the class="perma-info" in your DOM or you get some result in which case you continue with:
console.log($('.perma-info').text());
and then observe the console. If you get an empty text then the if condition should work. If it prints some value then the DOM element that was matched had some text.
Happy debugging.
Press F12 and set a breakpoint.
Open up your Google Chrome Developement tool and click on Scripts
Select the correct script file and set the breakpoints you want (on the if-statement preferrably)
Start running the script!
Devtool will stop on the breakpoint. You can see global and local variables. You should store the text-value to a variable in order to see the actual content of the variable.
Follow others instructions to get to the dev tool but I think inside of your li there is a space. I use firebug on firefox and I saw a space.
try
if ( $.trim($('.perma-info').text()) == '') {
$('.perma-info').remove();
}
I have stored some textbox values in cookies.
But if textbox blank then cookie value will "null"
I am assigning that cookie value to another textbox.But due to the null value of cookie it showing me "null" in textbox.
I want to replace "null" with a blank space.
Make uf of .val() function will do you task.
$('#youtextboxid').val(valueneedtoset);
Try with
$('input[name="Custom_Field_Custom7"]').val('11111');
It's hard to tell based on what you've said, but I think the problem might be that you are trying to access the textbox before it has loaded. Try something like this:
$(function() {
// your code here
});
And as others have mentioned, it would be a lot easier to just do this:
$('input[name="Orders\\.Custom_Field_Custom1"]').val($.cookie('UploadFile',{ path:'/'}));
I'm trying to get the innerHTML value of a node. The value is D&O. When I try to get this value using innerHTML I'm getting D &[semicolon] O. Is there any option to get the exact value rather than encoded value using Javascript? Please help me.
Forum prevents me from entering semicolon after &
You can use
return ("innerText" in node) ? node.innerText : node.textContent;
innerHTML will returns the equivalent HTML that would provide the text that you see.
element.firstChild.data;
Get the data in the text node contained in the element. If you want the text and not HTML that represents it, don't use innerHTML.