Trying to select the Export button below which has an ID and name. I know I can getelementbyID but not sure how to then specify to further drill into the getattribute("name") because the getelementbyID wil only return 1 value, not an array
Thanks!
#Mturks83
getElementsByName("export")(0) the first part will give you the array of elements called export the (0) gives you the first.
It is faster to use querySelector than getElementsBy.
For example, just got direct with an attribute = value selector
ie.document.querySelector("[name=export]")
If more than one then add the parent td element id
ie.document.querySelector("#bottomButtonRow [name=export]")
Reading:
css selectors
Related
$x("//div[#class='card-info__container']/div[3]/a")
using this I've found the container, but now I can't get the specific element I need.
when I enter this the response is:
⯆(3) [a.cta.btn, a.cta.btn, a.cta.btn]
⯈0: a.cta.btn
⯈1: a.cta.btn
⯈2: a.cta.btn
how can I take only the first one?
You can use the at() method to select the first item from the array of a that is returned from executing the XPath:
$x("//div[#class='card-info__container']/div[3]/a").at(0)
For an XPath that returns a list of elements,
xpath
you can select only the first element via indexing:
(xpath)[1]
I do not really understand the difference between these three ways to declare an ID in html:
[id] = "'example'"
id = "example"
#example
The first two seem to be identical, is this correct?
These I can style in my example.component.css file.
The third one is special. I understand I can use it everywhere in the current html view, but I cannot apply CSS styles with example.component.css, is this correct?
Which one shall I use in angular? A combination of 1/2 and 3?
I also noticed if I use the same ID in different components, I will have duplicate ID's, which is really bad, so eventhough I use angular and different components I must be very careful how I name ID's, is this correct?
Version 2 is the default html syntax for an id
Version 1 is the angular way, if the id is a variable, e.g. [id]="myId"
Version 3 is the angular way to export/reference a html element to angular. This is not an id.
The id is a HTML Element (not angular), so you have to look that the id in html after building is unique.
Yes the first two have an identical end result. The second one is a string while the first one is a javascript expression and is evaluated by angular. This means you can use things like component properties such as [id]="'example-' . foo" which outputs id="example-2" if you had a property foo = 2; in your component.
The third one actually doesn't have anything to do with the ID attribute in HTML, but I understand why it may seem like it. It's actually a template reference variable and it allows you to access this element from anywhere else in your template, or even from your component code.
You're right, the html specification requires an ID to be unique, browsers are forgiving so they may permit you to use duplicate IDs but it should be avoided at all costs.
You can use id="unique_id" if you don't want to change it dynamically. If you want to change your HTML element id dynamically through Component.ts then you should use [id]="your_variable" & #example serves for different purpose described below.
id with [] brackets is angular directive to set HTML id attribute value through a variable or expression
id is a HTML attribute which sets a unique id on an element
#example if you are writing like this in Component.html you are basically creating a template reference variable which is a reference to a DOM element within a template. You can then access this using Angular #ViewChild decorator. It can also refer to a directive (which contains a component), an element, TemplateRef, or a web component.
Angular Template Reference Variable
[id] = "'example'" => This one set assign example as id and it is same as id = "example".
Suppose if we want to assign a variable value then use [id] = "example".For this in ts file you have to declare the example variable
Public example ="your-class-name";
So id value will be your-class-name
#example => These are templare refference variable.
A template reference variable is often a reference to a DOM element within a template.
For example,
<input #phone placeholder="phone number" />
<!-- lots of other elements -->
<!-- phone refers to the input element; pass its `value` to an event handler -->
<button (click)="callPhone(phone.value)">Call</button>
I am trying to capture a HTML element, for the purpose of sending the value into GA as an event. I am using GTM, and want to use a click trigger to push this HTML value into a variable.
For example, the tags are set up like this:
<div class ="xxxyyyyzzzz" value1="qwejsdkfj" value3="akhdfjksh">
<div class ="fjk" >
<h1> "xyz2"</h1>
with each level nested under the other. The value we want to capture for GTM purposes is the one that sits under h1 ("xyz2"). Is this possible?
You need to select the value in JS and transmit it to GA via event (or the initial pageview, if you want to hold the number of requests low).
Method without GTM:
Select the value of your HTML tag via selector
// select the value via CSS selector, catch the first result of your query
var selector = document.querySelectorAll(".fjk h1")[0];
// select your category, action and label (I set label with the value of your selector
if(selector) ga('send', 'event', [eventCategory], [eventAction], selector, [eventValue], [fieldsObject]);
Method within GTM:
Create a variable which holds the information of your HTML tag. Use "CSS Selector" in variable definition and use the CSS selector I mentioned above.
Use the variable within your Universal Analytics Tag definition. You can set the value of a custom dimension index with the value of your defined variable.
I have an issue I am trying to select multiple CSS classes / ids.
The way my theme calls multiple buttons is it adds +1 to the button id like so:
edit-add-to-wishlist
edit-add-to-wishlist--1
edit-add-to-wishlist--2
edit-add-to-wishlist--3
etc
I wish to make said buttons all float to the right, without having to individually name and create each id in the CSS.
I have attempted to use the nth:child option as well as the * universal selector without much success.
Is selecting multiple a range (I guess you would call it) possible?
Something like this:
#edit-add-to-wishlist--* {
float: right;
}
You can use the attribute selector: [attr*=value]:
[id*=edit-add-to-wishlist] {
color: red;
}
jsFiddle here - it works
It selects all instances where a id contains edit-add-to-wishlist
[attr*=value]
Represents an element with an attribute name of attr and whose value contains at least one occurrence of string "value" as substring.
MDN documentation on the attribute selector
button[id^=edit-add-to-wishlist] {float:right}
Uses CSS3's substring matching by looking for <button id="edit-add-to-wishlist*> where the asterisk is a wildcard.
I've got a select element in my HTML page and I'd like to do a specific action using Mootools when a particular value of this select is chosen but I don't know how to access the element.
My element has cars_number as id and to access value 1 of this select element I've tried cars_number[1] but it doesn't work.
Try with:
$('cars_number').getSelected();
MooTools Documentation