.next('tag') in plain javascript? [duplicate] - javascript

This question already has answers here:
Get next element with specific class of clicked element with pure js
(3 answers)
Closed 4 years ago.
Is it possible to search for the next sibling-element with a specified tag?
.nextElementSibling is not a function so... can't pass any tag with it?
jQuery
$(event.target).parent().next('div').attr('id')
Java Script
event.target.parentElement.nextElementSibling.getAttribute('id');

you should try using querySelectorAll https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll
and then simply just looping over the results for your desired result. Please have a look at the following js fiddle
https://jsfiddle.net/c0bdgxtj/12/
var wrapper = document.getElementById("wrapper");
var debugEle = document.getElementById("debug");
var desiredElements = wrapper.querySelectorAll('div[data-test="y"]');

Related

How to read data between xml tags with attributes in javascript/ionic without using DOM/ActiveXObject? [duplicate]

This question already has answers here:
Parse XML using JavaScript [duplicate]
(2 answers)
Closed 5 years ago.
I simply need to extract some info between two tags, in this case, <wsse:BinarySecurityToken>
For example:
<wsse:BinarySecurityToken att1="abc" att2="cd3" att3="adfa">This is a text I need!!!===</wsse:BinarySecurityToken>
I tried
match = text.match(/<wsse:BinarySecurityToken[.*]>([^<]*)<\/wsse:BinarySecurityToken>/g)
does't work!
Or is there anything better than regex? I use angularJs 1
You want to do:
match = text.match(/<tag1.*>([^<]*)<\/tag1>/g)

Is there a way in plain javascript to select element in options by value? [duplicate]

This question already has answers here:
Find an element in DOM based on an attribute value
(11 answers)
Closed 6 years ago.
As you can see from the title, I am wondering if there is a way in plain javascript to select an element from option fields by some value. I know I can do it with jQuery like this - option[value="'+ myObject.value+'"] but I want to know if I can do the same with javascript without using some loop to find the index and then select it by its value.
P.S. I guess jQuery is doing exactly this^ but I am not sure.
You can use querySelector with css attribute equals selector.
var ele = document.querySelector('option[value="' + myObject.value+ '"]');
You can do it as easily in plain JavaScript by using document.querySelectorAll:
document.querySelectorAll("option[value='" + myObject.value + "']");

How to combine multiple find() in jquery? [duplicate]

This question already has answers here:
jquery multiple attribute selector problem
(3 answers)
Closed 6 years ago.
I have these two conditions:
1) Find all div with a "data-filter-program" attribute which equal to something
2) Find all div with a "data-filter-expertise" attribute which equal to something
And I need to somehow combine these two into one statement in jquery. I would like something like this:
mentors = $("div").find("[data-filter-program*='"+selected_program+"']" && "[data-filter-expertise*='"+selected_expertise+"']");
How do I actually achieve this correctly in Jquery?
Thanks!
mentors = $("div").find("[data-filter-program*='"+selected_program+"'], [data-filter-expertise*='"+selected_expertise+"']");
Just add a comma between each item you would like to find.
$("div").find("#apples, #bananas, .grapes, div");

Get element by not having an id/class [duplicate]

This question already has answers here:
How to Select Element That Does Not have Specific Class
(6 answers)
Closed 7 years ago.
I want to select all elements that don't have a specific class, how can I do this ?
I would like to have something like :
var test = document.getElementsByClassName(!"class");
Use
var test = document.querySelectorAll(":not(.class)");
But you'd better be a little more precise, for example targeting only divs:
var test = document.querySelectorAll("div:not(.class)");

How can I change the text using JavaScript [duplicate]

This question already has answers here:
How do I change the text of a span element using JavaScript?
(18 answers)
Closed 7 years ago.
<div class="basePrice">$99.99</div>
I want to change the value $99.99 using JavaScript.
I tried using getElementByClass but it didn't produce the results that I was hoping to get.
document.getElementsByClass returns a NodeList, which is kind of like an array.
You have to specify which element (there's only one here, so I'm assuming the first) you're looking for, and then you can use .textContent to change the text of the node.
document.getElementsByClassName("basePrice")[0].textContent = "$49.99";
document.getElementsByClassName("basePrice")[0].textContent = "$49.99";
<div class="basePrice">$99.99</div>
You can do that like so
document.querySelector('.basePrice').innerHTML = 'different text'
FIDDLE
try getElementByClassName I believe is the proper syntax.

Categories