jQuery how to use extra select criteria on a parent node? - javascript

I want to apply a select criteria to a node I have fetched using a jQuery:
let objDIV = $("#selindividual").parent();
Now that I have the DIV I want to apply the following to it:
button:contains(\"Submit\")
If I was writing this in a node that contains buttons the query might look like this:
$("#idofnode button:contains(\"Submit\")")
So the question is how do I do the above when I have the node already?

You can use find():
let $objDIV = $("#selindividual").parent();
var $button = $objDIV.find('button:contains("Submit")');
children() would also work, it would just depend what relation the button is to the selected element and how far you want to traverse down the DOM to find it.

Related

Nightwatch dropdowns handling by passing value

Below is the approach I have used in order to select values from a dropdown using nightwatch.As you can see this is not a good approach. We can't select the specific value from dropdown unless we click on the exact element.
this.useXpath();
this.click('(//td[#class="styles_selectDropdownContainer__2Vrns"])[1]')
this.useCss();
this.click('#react-select-6-option-1')
In selenium java there is a very good option like below
Select fruits = new Select(driver.findElement(By.id("fruits")));
fruits.selectByVisibleText("Banana");
I want to know of there is a similar approach can be used in nightwatch as well?
This is not built up using Select and Option tag so inbuilt selenium functions wouldn't work. Work around would be to click first on the parent span and then in list store every div (which is option), iterate the loop and for each web element if text matches with your desired text you can click on it.
Code :
this.useCss();
this.click("span[aria-live='polite']")
Now store options in a list :
var elements = document.querySelectorAll('.elements'); // use
//div[contains(#class,'option')] as element selector.
Now iterate the list :
// Iterate over them.
[].forEach.call(elements, function (element) {
// Manipulate each element.
element.click();
});
});

jQuery: How can I get the class of a certain element that's inside another element?

Lets say I have a table named unitTables with some rows. It is one of many similar tables on the webpage. In those rows is a class called selected, which I want to retrieve for a variable. The other tables on the page also use the selected class, so I need to retrieve this specific instance of selected.
In plain JavaScript, the way to find it is:
var table = document.getElementById('unitTables');
var selected = table.getElementsByClassName("selected");
What's the equivalent to the above two rows in jQuery?
I know I can rewrite the first row like this:
var table = $('#unitTables');
But I know I can't do the exact same thing to the next row, as I need to use to id of unitTables in order to get the specific selected row in that table. Currently I have:
var selected = from.$(".selected");
Which throws an Uncaught TypeError: from.$ is not a function. error, so I know that's not the correct syntax.
What's the best approach?
var table = document.getElementById('unitTables');
var selected = table.getElementsByClassName("selected");
What's the equivalent to the above two rows in jQuery?
var selected = $("#unitTables .selected");
(Except selected will be a jQuery object around the matched set of elements, instead of a collection.)
The jQuery function (jQuery or [usually] $) looks up elements via CSS selectors. So in this case, a descendant selector that looks for .selected within #unitTables.

JQuery find element that has specific value without loop

I am trying to figure out if there is a way (without using any loops like each etc..) to look up element that has specific value, something like
var myElment = $('.container');
var child = $('.container-child');
myElement.find(child /* That has value of test */);
value is related to .val() of child element, so text inside it.
By the Attribute Equals Selector which is [attr=value].
So, to do that, just use
$(".container .container-child[value='test']")

javascript elements/tags array DOM node access

what's the different between using:
// assuming using elements/tags 'span' creates an array and want to access its first node
1) var arrayAccess = document.getElementsByTagName('elementName')[0]; // also tried property items()
vs
// assuming I assign an id value to the first span element/tag
// specifically calling a node by using it's id value
2) var idAccess = document.getElementById('idValue');
then if I want to change the text node....when using example 1) it will not work, for example:
arrayAccess.firstChild.nodeValue = 'some text';
or
arrayAccess.innerText/innerHTML/textContent = 'some text';
If I "access" the node through its id value then it seems to work fine....
Why is it that when using array it does not work? I'm new to javascript and the book I'm reading does not provide an answer.
Both are working,
In your first case you need to pass the tag name instead of the element name. Then only it will work.
There might be a case that you trying to set input/form elements using innerHTML. At that moment you need to use .value instead of innerHTML.
InnerHTML should be used for div, span, td and similar elements.
So your html markup example:
<div class="test">test</div>
<div class="test">test1</div>
<span id="test">test2</span>
<button id="abc" onclick="renderEle();">Change Text</button>
Your JS code:
function renderEle() {
var arrayAccess = document.getElementsByTagName('div')[0];
arrayAccess.innerHTML = "changed Text";
var idEle = document.getElementById('test');
idEle.innerHTML = "changed this one as well";
}
Working Fiddle
When you use document.getElementsByTagName('p'), the browser traverses the rendered DOM tree and returns a node list (array) of all elements that have the matching tag.
When you use document.getElementById('something'), the browser traverses the rendered DOM tree and returns a single node matching the ID if it exists (since html ID's are unique).
There are many differences when to use which, but one main factor will be speed (getElementById is much faster since you're only searching for 1 item).
To address your other question, you already have specified that you want the first element in the returned nodeList (index [0]) in your function call:
var arrayAccess = document.getElementsByTagName('elementName')[0];
Therefore, arrayAccess is already set to the first element in the returned query. You should be able to access the text by the following. The same code should work if you used document.getElementById to get the DOM element:
console.log(arrayAccess.textContent);
Here's a fiddle with an example:
http://jsfiddle.net/qoe30w2w/
Hope this helps!

Adding and finding classes that is dynamically added to body - jquery

Is there any way to add classes or alter objects that is dynamically added to the body?
I am adding a range of objects to the body by javascript.
Fot instance Im adding some links that is dynamically generated and added to the body. When they are loaded I need to elect the first of the divs and add a new class to it.
I can't seem to find any way to do this... Update the DOM or how should I go around this? There must be a way to alter dynamically added objects.
Any clues?
Thanks for any help!
if you added them dynamically, then you can just use the objects you already have. Otherwise you'll need to find them with sizzle.
//create the elements
var $link1 = $('<a>click me</a>').attr('href','page1.html');
var $link2 = $('<a>click me</a>').attr('href','page2.html');
//append the elements
$('#some-links').append($link1).append($link2);
//use the element we created
$link1.addClass('my-class');
//find the second link element using sizzle
$('#some-links>a').eq(1).addClass('my-other-class');
demo: http://jsfiddle.net/PtebM/2/
Well of course you caN:
var a = $('<a>');
a.html('new link').appendTo('#menu');
a.addClass('border');
or
var a = $('<a>');
a.html('new link').appendTo('#menu');
$('#menu a').addClass('border');
fiddle http://jsfiddle.net/4NPqH/
Why not just add a class name to your generated elements?.
I.e.:
$('span').html('Hello world').addClass("fancyText").appendTo('body');

Categories

Resources