I've problems to clear inputs text width jQuery - javascript

Well.... i've the next problem and i don't know for why....
I need clear some inputs, the value and text but the text in the input not clear.
My code:
$("#bloque-addDireccion").each(function(){
var input = $(this).find("input[name$='Mia']");
$(input).each(function(){
$(this).text("");
$(this).attr("value","");
});
});
I'm inspect the inputs and the value it's correct ("") but i've the older text always in the input.
What i'm do wrong?

The .text(text) method cannot be used on form inputs. To set the text value of input or textarea elements, use the .val(value) method.
please also check the Difference between val() and text()
try .val(value)
$(this).val("");

As others mentioned to change the value you need to do $(this).val("");.
Me being a beginner as well, my guess why .text() does not work is because as mentioned here: http://www.w3schools.com/tags/tag_input.asp, <input> does not have a text attribute, therefore at this example, $(this).text() seems to point at nothing.

Well... My solution: I chage the value in other javascript using .value("xxxxx"); to change the value and I change this for
.attr("value","xxxx");
And, when I try to reset/change next time the value, i'm using
.attr("value","xxxx");
and it's works....If put .val("xxx"); isn't works!
Thanks for all responses! :D

Related

JS, How to set textarea values

Can anyone tell me how can I set textbox value?
Site: https://signup.live.com/signup
I tried this one
document.getElementById("MemberName").value="Luxury_UAE_EA#hotmail.com"
document.getElementById("iSignupAction").click();
I suppose you don't mean textbox but textarea instead.
Just give the textarea an id and store the desired value on the element which you had tried to achieve.
document.getElementById("MemberName").value="Luxury_UAE_EA#hotmail.com"
<textarea id='MemberName'>test</textarea>

Loop through visible input elements in a form in Javascript?

I'm trying to loop through all visible inputs within a form and set their value to be empty. What I have doesn't seem to work for text inputs, it returns undefined. Any ideas how to do this?
jQuery has a :visible selector as well as an :input selector. In addition, most jQuery methods operate on the entire set. val() can be used directly rather than looping through the set.
currentForm.find(':input:visible').val('');
Textbox inputs would have a tagName of "textarea". Not sure why the other text types aren't working. Have you tried:
childs[i].values = '';
?

How do I get the title attribute of an option tag that I am setting in it using my xml

I need to populate the dropdown and onchange of this dropdown I have to set the values in two text fields. I have set the value attribute for option tag and I can use it to put this in my first text box. Now my second textbox should have the value coming from the same xml. I cannot use value attribute twice in xml, so what I did was that I added an title attribute to this option tag. And now I want this title to be put in my second text box, but it doesn't happen. here us what I am suing:
$("#country").change(function() {
$(".firsttextbox").attr("value",$(this).val()); //this works
$(".secondtextbox").attr("value",$(this).attr('title').val()); //this doesn't work
})
$(".secondtextbox").attr("value",$(this).find("option:selected").attr("title"));
Added this line to solution, Example can be found at http://jsfiddle.net/pHpr2/
Using $(".secondtextbox").attr("value",$(this).attr('title').val()); fins the <select title="title"> that is the issue was with you.
I'm a little confused by what you're trying to do here. I think, however, that the solution is probably simple.
attr retrieves the attribute (or property, in some cases) as a string (or at least it does for the title attribute) so there is no need to use val() on it – indeed you can't!
$(".secondtextbox").attr("value",$(this).attr('title'));
Note that it would probably suffice simply to use this.value and this.title, depending on whether the code is in fact XML or if it's HTML.
Instead of $(this).attr('title').val(), try using only $(this).attr('title')

Getting HTML with the data using jQuery

I have a form which has many elements (e.g. textarea, input, select), after users have entered some data the states of these elements should change.
For example, an input[type="radio"] element will have the attribute checked="checked" if a user has checked it. The value attribute of an input[type="text"] element will contain the text entered by user.
The problem is that the html string returned by $('#form1').html() does not contain these data.
Feel free to take a look at this example:
http://jsfiddle.net/cmNmu/
You can see that no matter what your inputs are, the html returned is still the same (having no attribute data).
Is there any easy way to collect the html including their states?
Thanks in advance.
use below code getting the value of input type text via jQuery
alert($("input:text").val())
Maybe you could use the 'onblur' event handler to set the value of the element when you leave it
You should get the value using :
$('#form1').find(':input').val();
$('#form1').find(':radio[name=gender]:checked').val();
if you have multiple input then you can filter them bu their name or class or even id. Then you will need to select input using .find(':input[name=input_field_name]'). My Suggestion is : use name property instead of other property if you want to use form.
People usually use $('#form1').serialize() to get the values. If html() doesn't return both the source and data, I don't think that there is something you can other than manually constructing the full html by looking at the data.
Live demo: http://jsfiddle.net/cmNmu/6/
By using the jQuery formhtml plugin written by gnarf:
jQuery html() in Firefox (uses .innerHTML) ignores DOM changes
The changes in the input elements can be reflected in the html string returned by formhtml().
Thank you very much everyone.

How to disable an input with jQuery Validation Plugin

This should be really simple but I can't get it to work, how do I disable and add a class to an input? Let's say I've got an input field with id = name, this is how far I got,
$("input#name").attr("disabled");
What am I doing wrong here? Any help would be greatly appreciated, thanks in advance!
You need to give .attr() a second parameter (the value to set, otherwise it gets the value), true in this case ("disabled" works too, but a boolean is desirable for a few reasons here):
$("input#name").attr("disabled", true).addClass('myClass');
.addClass() then adds that class to the element.

Categories