Get value of name attribute in form - javascript

I'm trying to get the value of a name attribute in my html form.
I tried:
$('input[name=username]').val();
but that doesn't work it.
Hope someone can help me.
edit:
Sorry I wasn't really clear but I want to get the input value of the form.
When I try:
GM_setValue ("username", $('input[name=username]').val() );
and check about:config after I fill in the form the value of username is empty
Thanks!

What you have done will get you the value of the input with name=username. If you want to get the value "username" (not sure why, but I think it is what you are asking) then you can do this:
var nameValue = $('input[name=username]').attr("name");
Assuminge the following HTML:
<input name="username" value="my value" />
Your original method will return a result of "my value", my method will return a result of "username". If I have misunderstood your requirements then please let me know what you are trying to do, as what you already have should work fine.
Here is an example of both in action

you can use this to get the name
$("#id").attr("name");

Related

How to collect a string from a radio input and send it to a useState? (React) [duplicate]

I have a MVC3 app using Project Awesome (http://awesome.codeplex.com/), but I am getting a weird behaviour on checkboxes. I have the following simple Html within a Modal popup <input type="checkbox" class="check-box" name="IsDeleted">
When I submit the form containing this element, its post value is 'on' instead of the expected 'true' (when element is checked).
Does anybody know why this is? I am assuming there may be some javascript somewhere messing with the form data, but wanted to check whether there isn't some HTML I am missing.
Thanks
Set the checkboxes value attribute to true and you will get true in your post value.
It's browser specific, I suppose, what to send when value is undefined. You need to defined value attribute on your radios/checkboxes to be sure what will be passed back to you. I would suggest value="1"
set data-val="true" and value="true" by deafult...
if checkbox is checked then returns true
Check Checkbox is checked or not if checked set Hidden field true else set hidden field false.
$('#hiddenFieldId').val($('#CheckBoxId').attr('checked')=='checked')
Surely you should just check if it is set - the value that it sends across is irrelevant, if it's not checked, then nothing at all gets sent when you POST.
Nothing worked!
I ended up on a hacky way after seeing the serialised form object just before posting to controller/action. Its not safe in case if anyone would have any textboxes inside that may contain ampersands. In my case, i had an array of checkboxes, so I did this hack after I am very sure, i won't have problems.
var formData = $("#form").serialize();
formData = formData.replaceAll('=on&','=true&');
if (formData.endsWith('=on'))
{
formData = formData.substring(0, formData.length - 3) + "=true";
}
Hope it helps to those 'someone' with my scenario. Happy hacking.
Use jQuery for cross-browser decision. And it will return true or false anyway.
$('#check-box-id').attr('checked' ) == true
if your checkbox has an id check-box-id. For your current version use the next (select by class name):
$('.check-box').attr('checked' ) == true
Use jQuery
var out=$('.check-box').is('checked')
If checkbox is checked out=true
else out=false
In HTML, add a input type="hidden" above checkbox:
<input name="active" id="activeVal" type="hidden">
<input id="active" type="checkbox">
Then, add a script as below:
$('#activeVal').val($('#active').is(':checked'));
$('#active').change(function() {
$('#activeVal').val($('#active').is(':checked'));
});
When you do eg. $('#your-form').serialize() in jQuery, you will get value of checkbox when checked active: true or uncheck active: false

How to get an element value that is not showing in html code with Cypress?

this is the code of my element
<input type="text" class="nameOfClass" id="someid" name="somename" maxlength="255" placeholder="justholder" ng-model="model" tooltip-placement="top" tooltip-trigger="mouseenter" tooltip-animation="false" style="">
as you can see there is no attribute value, but I can clearly see that there is text in that text field in web app I am trying to automate.
So my problem is, that I don't know how to get value of the text field.
I've tried google chrome inspector to find where is the value but without any luck. Somewhere I read, that caching can causing this problem, but in the network console I can see the values in request response.
Thanks
Assuming that you want to get the value written in the text field, you can get it by invoking val.
cy.get('#someid')
.invoke('val')
.then((value) => {
cy.log(value) //logs the value
})
If you want to apply any assertion on the value, you can:
cy.get('#someid').should('have.value', 'your-value')
If you are referring to type="text", that's not the text the user types in - it's an attribute that tells the input what values to allow.
You can also have type="number", type="date", type="color", etc
Checking that input is of type "text" would be done with this,
cy.get('input#someid').should('have.attr', 'type', 'text')
Checking the value property would be done like this
cy.get('input#someid')
.type('entering a value') // there's nothing in value yet
.should('have.value', 'entering a value')

How to change hidden field to true when someone enter text in another field

I'm working with django templates... want to do something like this in JS:
If someone entered a text to textbox (id=super_text), I want to automatically change a boolean field hiddenInput (id=super_boolean) value to True.
I'm totally new in JS, was learning django backend for few months but I got my first project and one task is to do something like this in JS.
Can you help please? I will be thankfull (;
In HTML, <input>s can't be of type boolean, but the code below sets the text value of the hidden input to 'true'. (Even though I use the boolean value true, JS converts it to the string "true" when storing it in 'value', since the DOM knows that the input can only have a string as a value.)
<input type="text" id="super_text">
<input type="hidden" id="super_boolean">
<script>
document.getElementById('super_text').addEventListener('keypress', function(){
document.getElementById('super_boolean').value = true;
})
</script>

how to set default values on load in jquery form?

I am using jq-idealforms to create form. Currently I am not using dynamic method to create any field. on certian event, I would like to set value in all the fields in the given form.
In other words, I want to preload the form with specific values on certain event. I am not able to find any such kind of documentation. So need help
Demo form : http://bit.ly/1ahZalu
I do not know if I understood well, but maybe this can help
http://jsfiddle.net/ymfvqyob/2/
var set=[]; //array name:field value, ...
set.push({'username':'test username 1','email':'test email 1'});
set.push({'username':'test username 2','email':'test email 2'});
//here is a click event used to call functions setVal
$('#set1').click(function(){
setVal(0)
})
$('#set2').click(function(){
setVal(1)
})
function setVal(ind)
{
$.each(set[ind],function(name,val){
$('form input[name="'+name+'"]').val(val);
})
}
In the HTML, just use the "value" attribute for text, and the "checked" attribute for radios and checkboxes:
<input type="text" value="John"/><br/>
<input type="checkbox" checked/>
As you can see, these automatically have specified values.
I hope this helps!

javascript - Need to pull data out of a string

Hi I have a datatables based table that holds data. I need to be able to get the values out of the following string that I have been able to do so far.
<input name="jobNo" value="job_no_123" />job_no_123
I need to be able to get the name and the value and store into separate variables that I then pass off to do something else.
However I do also have another 4 fields on the page that I need to capture too:
<input name="item_1" value="data1" />data1
<input name="item_2" value="data2" />data2
<input name="item_3" value="data3" />data3
<input name="item_4" value="data4" />data4
And of top of this, this would only be the data from one row, and I need to do this for multiple rows too. But I need to start somewhere.
Please help.
Thanks
I am not sure what are your needs but here is a code that should help
$('input').each(function(i, val){
$(this).attr('name'); // will get the attribute name
$(this).attr('value'); // will get the attribute value
$(this).attr('text'); // will get the text inside the input
});
This regex can find the values from your code. However, it assumes that the input is strictly in that format, with no variations in even whitespace.
<input name="(.*)" value="(.*)" />
The values will be in captured groups 1 and 2.
Note: I am aware that regex should not be used to parse HTML, but if the input is strict enough, it works.

Categories

Resources