Select with Intern over CSS Selector ID Input Field - javascript

Hey i want to select with Intern over CSS Selector ID this input field and put some text in.
<div class="controls input-group has-success">
<input id="billingAddress.firstName" class="form-control valid" type="text" required="" value="" name="billingAddress.firstName" from="1..3" placeholder="" autocomplete="given-name">
I try this commands but nothing works for me:
findByCssSelector('form input[name=billingAddress.firstName]')
findByCssSelector('form input[name="billingAddress.firstName"]')
findByCssSelector('form input[ID=billingAddress.firstName]')
findByCssSelector('form input[ID="billingAddress.firstName"]')
findByCssSelector('input#billingAddress\\.firstName')
findById('billingAddress\.firstName')
findById('billingAddress\\.firstName')
Example Code:
findById('billingAddress\.firstName')
.click()
.type('test')
.sleep(200)
.end()
Could anyone possibly help me with that problem.
Thank you everybody for looking in my problem.

For findByCssSelector using attributes, the attribute value should be quoted, like:
.findByCssSelector('input[id="billingAddress.firstName"]')
For findById, you don't need to escape periods (the ID is just a string, not a regex):
.findById('billingAddress.firstName')
In a quick test with your HTML snippet, both of these work.

Related

In Cypress how to select input element based on name?

I'm starting to learn Cypress. I want to select the input field and provide the phone number by using cypress.io. The code I have following but it does not work. However can I using find or there is another way to get the input element to type in phone number?
cy.get('div').contains('Phone Number').find('input[name=teacher[0].number]').type('8000-1612023')
<div class="required field">
<label>Phone Number</label>
<div title="Teacher number" class="ui fluid right labeled input no-spinners">
<input required="" type="number" name="teacher[0].number" value="">
<div class="ui label label">US</div>
</div>
</div>
Why don't you directly target the input field using the following code
cy.get('input[name="teacher[0].number"]').type('8000-1612023')
Please find the screenshot below for a successful test. I would also recommend you to change the type of input in your HTML to tel
HTML:
<input required="" type="tel" name="teacher[0].number" value="">
Cypress:
describe('Trial', function() {
it('Test', function() {
cy.visit('http://localhost:8080/trials/')
cy.get('input[name="teacher[0].number"]').type('8000-1612023')
})
});
Test Result:
Try this instead:
cy.contains('div', 'Phone Number').find('input').first().type('8000-1612023')
The first argument to contains tells cypress to find the element with that selector that contains the text.
I write selectors with the input tag as below and it worked for me.
cy.get('input[name="elementName"]').type(val)
Check this to learn best practices when selecting elements:
https://docs.cypress.io/guides/references/best-practices#Selecting-Elements

change value of next element jquery

I have an input field and span like below and want to replace the text in the span.
<div>
<input autofocus="autofocus" type="email" value="" name="buyer_signup_email" id="buyer_signup_email" autocomplete="off">
//want to change below elements text
<span class="help-block red-text">will change this text</span>
</div>
I tried this but not working
$('input[name="buyer_signup_email"]').next().val('dd')
Could someone tell me whats wrong here? Thank you.
try using .text() instead of .val()
$('input[name="buyer_signup_email"]').next().text('dd')

How to set contents of a BootStrap Input Group using jQuery?

Below is an example of Bootstrap Code to create a input-group. How can I use jQuery to set the contents of the input-group using the id?
<div class = "container">
<div class = "input-group-addon">Your name</span>
<input type="text" class="form-control" id="inputField" placeholder="Full Name">
</div><br>
Also, is there a way I can make the input-group not editable. I want to use it to display read-only text.
$("inputField").???? <-- What goes here??
Thanks!!
$("#inputField").attr('readonly',true).val('myValue')
if you have access to the html put directly readonly="readonly" as attribute of input.
you can put value directly in HTML with the value attribute.
ps: dont forget the "#"
$('#inputField').val('the value');

Jquery selector input fill with same id but different number

<input id="value_1" type="text" name="values_1" value="somthing">
<input id="value_2" type="text" name="values_2" value="somthing">
<input id="value_8" type="text" name="values_8" value="somthing">
Hello everyone. I searched the forum and can not find solution for this. What I want is to catch input id by low to high number and fill it with text.
I have add button for fields
$("#value_1").val("something");
this method working for exactly id number
$( "span input" ).first().val("something");
this working great but i dont know how to catch second and third.
Please help and thanks in advance.
You can use :eq()
$('input:eq(0)').val(0);
$('input:eq(1)').val(1);
$('input:eq(2)').val(2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<input id="value_1" type="text" name="values_1" value="somthing">
<input id="value_2" type="text" name="values_2" value="somthing">
<input id="value_8" type="text" name="values_8" value="somthing">
I don't recommend this ID naming convention, but if you must,
// Iterate through input elements, checking if the beginning of the string is 'value_'
$("input[id*='value_']").each(function() {
if ($(this).attr('id').startsWith('value_')) {
// Do what you'd like with the matched input element, being $(this)
}
});
EDIT: This might work better for you:
$("input[id^='value_']").each(function() {
// Do what you'd like with the matched input element, being $(this)
});
Found here: https://stackoverflow.com/a/5413862/5169684

jQuery: check input with name attribute for hasClass

What I'm trying to do is check if input with name attribute "Name" has a class of "input-validation-error", if so apply css to the label with for attribute of "Name".
if ($("input[name='Name']").hasClass("input-validation-error"))
{
$("label[for='Name']").css("color:red");
console.log('here');
}
This doesn't seem to work. Any ideas?
Markup:
<label for="Name">Name</label>
<input class="input-validation-error" data-val="true" data-val-required="field is required" id="Name" name="Name" placeholder="Full Name" type="text" value="">
Instead of
$("label[for='Name']").css("color:red");
Try
$("label[for='Name']").css("color","red");
Read more on .css() here
OR
Instead of
$("label[for='Name']").css("color:red");
Try
$("label[for='Name']").attr("style","color:red");
The html provided in updated question and with above jquery modification its working see here.
As per the questioner comments below this answer , i think the questioner is adding HTML dynamically inside DOM so in that case use event delegation read more here.

Categories

Resources