I am trying to use document querySelectorAll to select an array of options. I found out that we cant directly use document.querySelectorAll as it gives me error
"ReferenceError: document is not defined"
and instead use it with browser.execute as mentioned in this answer but still getting undefined. The console statement logs undefined, it should have given me an array of elements instead. If I run this css selector in developer tools, it gives me the array I expect but not here. Any idea if I am doing something wrong?
browser.execute(function (data) {
console.log(document.querySelectorAll('div#question_' + quesNo + ' .answerBlock .answer-value'));
return true;
},[],null);
It may be due to the way you pass arguments to the browser.execute. Try the following:
browser.execute((data) =>
{
console.log(document.querySelectorAll('div#question_' + arguments[0] + ' .answerBlock .answer-value'));
return true;
}, [quesNo]);
Related
I am using the following 3 lines of code in my web app.
selected_build = event.currentTarget.parentElement.parentElement.id;
var jselect = $( "#" + selected_build + " .roomc" ).children(".room");
selected_room = jselect[0].id;
This works in most browsers but in IE 8 I get the error
'0.id' is null or not an object
which points to the third line of code. I assume this has something to do with parentElement only working on Elements, but I can't seem to figure it out.
I assume this has something to do with parentElement only working on Elements...
If IE8 doesn't have parentElement, since by definition you're dealing with an element as of event.currentTarget, you can safely use parentNode instead.
But as you're using jQuery anyway, why not...use jQuery?
selected_build = $(event.currentTarget).parent().parent().attr("id");
if (selected_build) {
var jselect = $("#" + selected_build + " .roomc").children(".room");
selected_room = jselect.attr("id");
if (selected_room) {
// ... use it
}
}
Note that I'm using .attr("id") to get the id. That's because if the jQuery set is empty, .attr("id") gives you undefined, whereas [0].id gives you an error.
And actually — you don't need to use ids at all here other than at the end for selected_room:
selected_room =
$(event.currentTarget)
.parent().parent()
.find(".roomc > .room")
.attr("id");
if (selected_room) {
// ... use it
}
...and I bet there's a way to hook up the event so we can avoid that .parent().parent(), too, which is fragile. But without seeing the structure, it's impossible to suggest something.
I've got some JS code here. Basically, I am trying to change the ID of an element to some value from a previous variable.
Here's what I got so far;
function() {
var colorarray = [ "RANDOMCOLOR_0", "RANDOMCOLOR_1", "RANDOMCOLOR_2" ];
var RANcolorarray = colorarray[Math.rsound(Math.random() * (colorarray.length - 1))];
document.getElementsByClassName('RANDOMCOLOR').setAttribute('id', RANcolorarray);
}
This code throws an error in Chrome for line 4: Uncaught TypeError: undefined is not a function which is weird because JsLint finds no errors.
I also tried using the other way to setting id's;
document.getElementsByClassName('RANDOMCOLOR').id = RANcolorarray;
However, although this method does not throw an error on chrome or jslint - it does not work at all after inspecting the element.. :/
Any ideas?
document.getElementsByClassName('RANDOMCOLOR') returns a list of DOM nodes (even if there's only one match) so you can't just call .setAttribute() on the list as the list doesn't have that method.
You can either get the first item out of the list and call .setAttribute() on that one or use a for loop to iterate through the list and call it on all of them. Of course, since you're setting the id, you should not be setting multiple elements to the same id, so I'll assume you just want one element:
document.getElementsByClassName('RANDOMCOLOR')[0].id = RANcolorarray;
Or, a little more safe:
var elems = document.getElementsByClassName('RANDOMCOLOR');
if (elems && elems.length) {
elems[0].id = RANcolorarray;
}
Storage.prototype.setObj = function(key, obj) {
return this.setItem(key, JSON.stringify(obj))
^-----Error in this line: Uncaught TypeError: Accessing selectionDirection on an input element that cannot have a selection
}
var selected = jQuery('input:checkbox.mychkbox:checked').each(function() {
return this.id;
});
sessionStorage.setObj("savedCollSearch",selected);
I am using jQuery 1.7.2 and Chrome 22.
This error shows up as Uncaught Exception in Firefox 16. Search in SO and Google does not help and I have no clue how to resolve this.
I am 100% sure jQuery is loaded properly.
This expression...
var selected = jQuery('input:checkbox.mychkbox:checked').each(function() {
return this.id;
});
... seems to be misused here: it'll return you a jQuery-wrapped collection of checked checkbox elements, which is probably not quite easy to stringify (because of circular references).
(as a sidenote, .each will stop the iteration at the first element which doesn't have an id, or have it set to an empty string, but that doesn't matter much here)
You probably wanted to use this instead:
var selected = jQuery('input:checkbox.mychkbox:checked').map(function() {
return this.id;
}).get();
Javascript/JQuery noob here, so apologies.
I'm using .ajax to get a JSON object then the code below to loop through and append the fields to the page. Easy.
$.each(data, function(i, item) {
$("#div-ipos").append(
"<img src=" + item.user.avatar_small_url + "> "
+ item.user.first_name
+ "<hr /><br />"
);
});
It works and the output is as expected, complete with the avatar path passed into an <img> tag.
However I get the following error:
TypeError: 'undefined' is not an object (evaluating 'item.user.avatar_small_url')
What do I need to do to that variable to make it behave properly in this context?
Use console.log(data); before your $.each to check what's in it. Most likely the server response contains an extra array element with no user. So the JSON could look like:
[{"user":{"avatar_small_url":"foo","first_name":"bar"}},
{"user":{"avatar_small_url":"bar","first_name":"foo"}},
{"something_other_than_user":9000}]
See how the last array element doesn't have a "user". If you have access to the server code, you may want to modify it so there is always a user, or you may modify your Javascript so that it exits early if the user field doesn't exist with something like: if(typeof item.user == 'undefined') return;
Sounds like either item.user is not defined, or item is not defined. Have you checked the value of item? I think you are expecting it to be something that it is not.
before fetching check using if condition whether key is present in that array as below
if( key in User)
{
User[key]
}
What to do when after all probing, a reportedly valid object return 'undefined' for any attribute probed? I use jQuery, $('selector').mouseover(function() { }); Everything returns 'undefined' for $(this) inside the function scope. The selector is a 'area' for a map tag and I'm looking for its parent attributes.
Your question is a bit vague, so maybe you can provide more details?
As for finding out about an object and the values of its properties, there are many ways to do it, including using Firebug or some other debug tools, etc. Here is a quick and dirty function that might help get you started until you can provide more details:
function listProperties(obj) {
var propList = "";
for(var propName in obj) {
if(typeof(obj[propName]) != "undefined") {
propList += (propName + ", ");
}
}
alert(propList);
}
That will display a list of the properties of the object that you pass it that are not undefined.
Hope that helps...
Is selector the name of the element? If so then you should reference it as:
$('area#selector')
or
$('#selector')
otherwise it will attempt to look for the (non-existent) "selector" HTML tag and, obviously, not find it.
Though this answer is a bit late, I'd still recommend checking out these links:
http://www.webweavertech.com/ovidiu/weblog/archives/000317.html
http://www.syger.it/Tutorials/JavaScriptIntrospector.html