Is there any way directly get the parent element to find its attributes?
In my situation, I have a
DOMElement img,I have to use functions
img.getparent().getparent().findElement(By.tagName("a")).getAttribute("href"));
and the result is not accuarate since a parent node can find many same type of elements
<td>
<img></img>
<a></a>
<a></a>
<a></a>
<a></a>
</td>
Cast DOMNode to DOMElement and use it's .getAttribute(String attr):
String href = ((DOMElement)(img.getparent())).getAttribute("href");
Related
How can i access a string array coming from a model class using sightly(HTL)
The TestModel is a model class that returns a string array , getResult() is the getter used to return the string array
how can I use sightly to get it??
<p>display output :</p>
<sly data-sly-use.object = "com.silversea.core.models.TestModel">
<sly data-sly-list.mylist = "${object.Result}"> //what command show we use instead of data-sly-list
<p>1st text: ${item} </p>
</sly>
</sly>
The problem you are facing here is caused by two things:
Defining an identifier on the data-sly-list statement allows you to rename the itemList and item variables. item will become variable and itemList will become variableList
More details in https://docs.adobe.com/content/help/en/experience-manager-htl/using/htl/block-statements.html
So in your example you must change ${item} into ${mylist}
<p>display output :</p>
<sly data-sly-use.object = "com.silversea.core.models.TestModel">
<sly data-sly-list.mylist = "${object.result}"> //what command show we use instead of data-sly-list
<p>1st text: ${mylist} </p>
</sly>
</sly>
The second thing is that you should also follow the java bean naming convention: So if you have a getter getResult() then in HTL you should use ${object.result} (starting from lowercase)
I'm trying to get the value of an input element e.g. this is simple form:
<form id="loginForm">
<label for="username">Username</label>
<input required type="text" class="form-control" id="username">
<label for="password">Passowrd</label>
<input required type="password"id="password">
</div>
<button type="submit">Submit</button>
</form>
with jQuery I would have written:
let value = $("#password").val();
or I could even serialize the whole form,
but in Dart this seems not working:
querySelector('#username').getAttribute('value');
, it returns null
I'm not using any frameworks,
any suggestions?
querySelector will only return an HtmlElement. Since you know the result is an InputElement (a TextInputElement at that) you need to cast it to gain access to the value attribute.
(querySelector('#usernames') as InputElement).value
As of Dart version 2.2.0 (2019), the above answers no longer appear to work. Casting now returns this error:
Uncaught CastError: Instance of 'HtmlElement': type 'HtmlElement' is not a subtype of type 'InputElement'
Here's a way to access an input field's value without casting:
String value = document.getElementById('username').text;
If you type hint the element as an InputElement when you use querySelector you'll be able to access its value.
InputElement cityInput = querySelector('#city-input');
final cityName = cityInput.value;
As of Dart HTML package version 0.15.0, this is the only way that worked for me
String value= document.querySelector('#movie_id').attributes['value'];
querySelector('xxx').attributes returns a map containing the string of value
I'm trying to compare two lists by using only scala templates from play framework. My goal is to show all items listA in a table and then find out if listA has objects containing the same values as in listB to change the appearence of the duplicated items.
So let's assume a.id has an equivalent b.id, then i want a.id to appear in the list but crossed-out.
Example Input: listA has 5 objects with the attributes name and id. listB has 2 objects with the attributes id and xxx.
Desired Output: I want to display all items from listA (using the first scala #for loop), but if the id already exists in listB, i want the item to be crossed out in my table.
This is what i got so far:
<tbody>
#for(a <- aList){
#for(b <- bList){
<thead>
<tr>
<th>Key</th>
<th>Name</th>
</tr>
</thead>
#if(a.id == b.id){
<tr>
<td><s>#a.id</s></td>
<td><s>#a.name</s></td>
</tr>
} else {
<tr>
<td>#a.id</td>
<td>#a.name</td>
</tr>
}
}
} </tbody>
So far the code works, but now every element from listA appears as many times as the second loop goes through, which absolutely makes sense, but how can i prevent that?
You should probably do this in Scala code, rather than inside the template.
What you want is listA with a Boolean for each element telling you if it's in listB. Here's a simple way to do this:
val listAWithBool = listA.map(a => (a, listB.map(_.id).contains(a.id)))
Then, in your for loop, you can do
#for((a,inB) <- listAWithBool){
#if(inB){
...
} else {
...
}
}
EDIT
Since you're using Play Java, most of what I wrote previously will be hard to put in code, since it's functional programming. A possible solution is to put all that in your template, which allows scala code; just put this at the top of your template (under the list of parameters):
#listAWithBool = #{
listA.map(a => (a, listB.map(_.id).contains(a.id)))
}
Now to explain the weird for((a, inB) <- listAWithBool) syntax: just as for(a <- listA) means "extract all values from my list and do the following with each of them, giving it the name a", this now means "extract all pairs of values from my list and do the following with each of them, giving name a and inB to its two elements.
Remember that we have built listAWithBool to be a list of pairs (as return type of the lambda) of type List[A, Boolean], so the compiler will be able to understand that a is of type A and inB is of type Boolean.
var valueA= document.getElementById(#a.id);
var valueB= document.getElementById(#b.id);
#if(valueA == valueB){
}
Or
You can use
if(valueA.contains(valueB)){
}
<tr>
<td width="70"><strong>Director:</strong></td>
<td><a href="/?&director=Zhangke Jia">Zhangke Jia</td>
</tr>
I try to do this parser but not working
foreach($html->find('strong.Director:') as $titlu) {
echo $titlu->find('a', 0)->plaintext;
}
In need some help please
It is not working because 'Director' is not a class name, It's just a text inside 'td > strong' tag.
These are the ways you can find html dom elements:
Finding HTML elements by id
Finding HTML elements by tag name
Finding HTML elements by class name
Finding HTML elements by HTML object collections
try this:
foreach($html->find('td a') as $titlu) {
echo $titlu->plaintext;
}
I wanna know how can I get a value of a child of a child. I have this...
<table id="table4">
<tr>
<td>Id:</td>
<td>Name:</td>
</tr>
<tr>
<td>1515</td>
<td>Thiago</td>
</tr>
</table>
In Jquery, I wanna do something like this...
var id = $("#table4:nth-child(2) tr:nth-child(1)").val();
How can I do this work? The first <td> of the second <tr>. I know that I can use "class" and "id" to make it easier, but in this case I can't do that for others reasons.
PS. Sorry about the bad English, I'm a noob in this language.
Thanks.
Like this:
var id = $("#table4 tr:nth-child(2) td:nth-child(1)").text();
You can test it out here. The :nth-child selector applies to the child, for instance the first is saying "a <tr> that is a second child of its parent", rather than your attempted version, which would mean "the second child of this <tr>".
Also, use .text() to get an elements text, rather than .val() which is for input type elements.
This isn't a fully jQuery solution, but just thought I'd point out that you could do this:
var txt = $("#table4")[0].rows[1].cells[0].innerHTML;
Or if there will be any HTML markup you want to avoid, you could do this:
var cell = $("#table4")[0].rows[1].cells[0];
var txt = cell.innerText || cell.textContent;