How can I retrieve the values of a variable in jQuery? - javascript

Hi guys I want to know how can I get the values of this code:
<input type="radio" name="pgto" value="entry1" class="payment" />
jQuery(".payment").click(function(e){
alert(e);
}
when I click in payment radio, the alert msg says [object object],
I try to search how can I get the values of the 'e' variable function,
I found this 'e.currentTarget' current Target, if I do something like e.currentTarget.value it will
alert de value entry1 if I do e.currentTarget.name it will alert 'pgto'.
What I want to know is what are the options for object variable 'e'.
I know that there is the currentTarget option. What are the others???
Like:
alert(e.something);

if you run this with firebug then you can see all the properties of e:
jQuery(".payment").click(function(e){
console.log(e);
});

You are grabbing every single element on the page that has the class payment. This might give you inputs, or divs, etc. You want that exact <input>, place a id on it.
You are attempting to alert the event-object as #Quentin has pointed out, read his comment
If you want to get the value of a input like this, you're going to have to write: $(*get input*).val()
If you wanted to test the printing out of an object, alert() is not the function to use. Try console.log( *object* )

Related

Grab input value after send key and console log the value Selenium Webdriver

I am currently working on a project where I need to integrate the use of the Selenium Webdriver. I am using the Chrome implementation of Web Driver and running it via Javascript. I am currently testing a simple quantity input form. I am having trouble with a particular aspect of this project and that is ... I need the test to run through the form and put in different values everytime. I am placing the values via the sendKeys function. Now the trouble starts here... I need to grab the value that the sendKeys function inputs into the field and console.log a message depending on the value.
If the value is over a 100 I need the test to console.log the message "Exceeds 100".
If the value is less than 0 I need it to console.log the message "Below 0".
And if there is no value I need it to console.log the message "No input".
It runs through and puts in new values just fine. But the issue has been grabbing the value and console.logging a message depending on the value. I've tried many different options but there's just so little documentation related to this exact topic. I will link my code below, and I appreciate any input you guys may have... because it has me stumped unfortunately.
Also I am curious if this can be done using assertions in any way...
Test File Below:
https://gist.github.com/anonymous/89a84dbc15ba4088719400be1f359045
There is a method getAttribute(String attrName) it will accept a string parameter, pass attribute name against which value got set.
for example:
WebElement element =driver.findElement("your unique element locator");
String valueText=element.getAttribute("value");
about the answer above me - you should try adding a .getText(), So the attribute value would become a String.
WebElement element = driver.findElement("your unique element locator");
String valueText = element.getAttribute("value").getText();
Please add the full error message, A screenshot of the console would be good.

Why cant I get the text boxes to POST/GET when I submit

Here is the dom:
This is how I am submitting:
function createOrderItems(){
showCustomPanel(" Creating Order Items ");
$("#order_items")[0].submit();
return false;
}
Here you can see that the text field isnt getting picked up:
You need name attribute on it to get it included in params
To add more detail to the answer already given, If you don't give the input a name then it has nothing to assign the information to that is entered into the text box.
Think of it like a variable
= "stuff"; wont do anything
this = "stuff"; will store stuff into this. Then you can use that information later.
Not really how it works on the back end but it is a good way of understanding it.

Dynamic onchange with Jquery

I am attempting to use jQuery to dynamically populate the onChange attribute in a text field to run multiple functions. However i get an error in the console stating
TypeError: required is not a function
When i type into the console required('test'); the function exists and returns true.
HTML
<input id="test">
jQuery
var list_of_functions = get_validation(); //this returns a string "required('test'); valid('test');"
$('#test').attr('onChange', list_of_functions);
Then when i go and change the fieldtext, I get an error that the function doesnt exist. Any help? Thanks.
Thanks to all the super helpful comments. I have found a better alternative to what I was attempting to do.
How to turn a String into a javascript function call?

run jQuery function by inline onClick and use function variable/reference

I am very new to this, but I wrote this and thought it would work first time, but I get an 'ReferenceError: Can't find variable: street' console error.
I get this error if I click on the 'Street' button.
It's quite basic but this is the first time I've made a function to use a var/ref from the onClick attribute.
Please see onClick markup...
Supersport
Street
Cruiser
Scooter
Motocross
Enduro
Kids
then please see my function, which gets the ref error above...
Also please note I am trying to use the onClick var/ref within my function so I can target specific elements relative to the button being clicked.
bikeFilter = function (y) {
$('.bike').fadeOut();
scrollTo(186);
$('.bike[data-group=' + y + ']').fadeIn();
bikeSliderNav();
bikeSlider();
return false;
}
Any expert advice would be really helpful.
Thanks in advance.
You'd probably wanna pass a String as input to your function and not the name of an undeclared and uninstantiated variable.
Try to use the single quotes to refer it as a String constant (you need single quotes since you are already using double quotes to tell your html tag the attribute value):
onclick="bikeFilter('scooter')"
Take a look here to see the difference in data typing in js, and here for a quick start about functions.
You should use them like :
onclick="bikeFilter('motocross')"
Don't forget to put ' around your parameters

javascript if condition not executing all commands

I'm having one little iritating problem. I have simple if condition in javascript code.
It goes something like this:
if (istinito)
{
alert ('123');
document.getElementById('obavestavanje').value="Pobedi "+ime_igraca+"!!!";
kraj=true;
}
Alert apears when istinito=true, but element with id="obavestenje" never get its value, and variable kraj never is set to true. Variable kraj is global variable, and there are no conflicts with other parts of the JS code.
Any ideas why code stops after alert?
Looks like document.getElementById('obavestavanje') is returning null. You are trying to de-reference the null reference by using document.getElementById('obavestavanje').value which results in null pointer exception. If you look into the console, you should see some exception being raised. It is always a good idea to check if the document.getElementById() is returning a valid object before trying to dereference it.
e.g.
if (istinito)
{
alert ('123');
element = document.getElementById('obavestavanje')
if(element){
element.value="Pobedi "+ime_igraca+"!!!";
}
kraj=true;
}
First advice i could give you:
Use more console logging for debugging. Almost any modern browser got a console to debug and other things.
if (istinito) {
console.log("i am here");
}
from that same console you can also execute commands. Those dom manipulations are easily done from the console. just run them and see if it works.
the code:
document.getElementById('obavestavanje').value = "some value"
looks ok. nothing wrong with it. i guess you don't have an element with id "obavestavanje" ?
Looks like your code is okay. And you are sure you have an element by id 'obavestavanje'. Could you please tell what element is it? Is it a button, textbox or someting like that?
Also the String in the "Pobedi "+ime_igraca+"!!!" , what is 'ime_igraca'? Is it a variable and if it is have you defined this variable somewhere?
Or did you mean to give the value "Pobedi ime_igraca !!!" ??
Thanks
Ranis MK

Categories

Resources