Get value from textarea. Using Javascript [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
When I try to take a value from a textfield, it works when I use this:
var name = $("input[name='Event[name]']").serializeArray();
name = name[0].value;
I can't get value from a textarea:
var desc = $("input[name='Event[desc]']").serializeArray();
Here is a link to working variant with a textfield. I tried with a textarea and I don't understand why it doesn't work.

Here buddy: http://jsfiddle.net/jVUsZ/
$(document).ready(function () {
var val = $.trim($("textarea").val());
if (val != "") {
alert(val);
}
});

$(document).ready(function () {
var val = $.trim($("textarea").val());
if (val.length !== 0) {
alert(val);
}
});

It is difficult to tell given that you have shown us only part of your code but:
var desc = $("input[name='Event[desc]']").serializeArray();
Your selector here only matches <input> elements. If you want to match a <textarea> element, then you have to replace input with textarea in your selector.
var desc = $("textarea[name='Event[desc]']").serializeArray();

Related

Comparing Two Textfield in jquery [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have two text fields in my form.
The following are the values of my two text fields
TEXTFIELD1 =
7369:7499:7521:7566:7654:7698:7782:7788:7839:7844:7876:7900:7902:7934
TEXTFIELD2 = 7566
How to check if the value of TEXTFIELD2 does exist in TEXTFIELD1?
Any help would be appreciated. Thank you
You can use string function includes
Assuming the ID of textfields are textfield1 and textfield2, :
var txt1 = $('#textfield1');
var txt2 = $('#textfield2');
if(txt1.val().includes(txt2.val())) {
//action
}
You would want to use Javascript IndexOf. It will return the index of the string found inside the first string. It will return -1 if it is not found. So, you that as the condition to check for the desired string.
let TEXTFIELD1 = "7369:7499:7521:7566:7654:7698:7782:7788:7839:7844:7876:7900:7902:7934"
let TEXTFIELD2 = "7566"
if (TEXTFIELD1.indexOf(TEXTFIELD2) > -1){
console.log("Text Found!");
}
else {
console.log("Text Not Found!");
}

Optimizing JavaScript Code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have written the following JS, and it gets the job done, but I have been having a ton of trouble eliminating the repeating code. I keep breaking the functionality.
It's just field validation using a regex, and adding an error message below the appropriate field if the input doesn't match the regex. It's works, but but I really want to begin writing more succinct code.
If someone can help me do this, I can compare mine and yours and start understanding how to approach this type of task.
Thanks.
<form></form>
Here's my JSFiddle code: https://jsfiddle.net/robinburrage/cnL2d4w8/2/
You can reuse the same function for all the 2 validations. The only difference between the 3 functions is that you are trying to append to a different id.
Use this instead of referring to the element specifically, since the input is anyways bound to the element you are looking for.
phone.addEventListener('keyup', validatePhone);
phone2.addEventListener('keyup', validatePhone);
phone3.addEventListener('keyup', validatePhone);
function validatePhone(evt) {
var pattern = /^(\(\d{1,2}\)\s)?\(?\d{4}\-\)?[\s.-]?\d{4}$/; //set the regular expression
var str = this.value; //get the user's input
var phoneVal = document.getElementById("phoneVal");
if (phoneVal) { //if the error message div is already on the page
phoneVal.parentNode.removeChild(phoneVal); //remove it, to prevent a series of error message divs from accumulating
}
if (str === "") {//if there is no user input
phoneVal.parentNode.removeChild(phoneVal);//remove the error message
}
if (pattern.test(str) === false) { //if the string doesn't match the expression
var elem = document.createElement("div"); //create a DIV
elem.setAttribute("id", "phoneVal"); //give it an 1)id, 2)class and 3)message
elem.setAttribute("class", "form-error-msg");
if(window.location.href.indexOf("/en") > -1) {
elem.innerHTML = 'Please enter a valid telephone number.';
}else{
elem.innerHTML = 'Por favor introduce un número de teléfono válido.';
}
this.parentNode.appendChild(elem); //add the div with the current error message
}
} //end function

Search function in HTML using JavaScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have tried to create a search function with Javascript, but it doesn't work.
function contains(text_one, text_two) {
if (text_one.indexof(text_two) != -1)
return true;
}
$("#searchText").onkeyup(function(){
var searchText = $("searchText").val().toLowerCase()
$("ul li").each(function() {
if (!contains($(this).text().toLowerCase(), searchText))
$(this).hide();
else
$(this).show();
});
});
You can debug all of these issues by yourself using the developer console. If you press F12 in your browser you will be able to see all the errors that your JS code is raising and address them.
From the above, I can see that you have several issues in your code:
You should use indexOf, not indexof, as JS is case-sensitive
The $('searchText') selector is missing the id prefix: $('#searchText')
jQuery has no onkeyup method, it's just keyup.
Also note that you can improve the logic by using toggle() and just returning the result of the indexOf comparison in the contains() function. Try this:
function contains(text_one, text_two) {
return text_one.toLowerCase().indexOf(text_two.toLowerCase()) != -1;
}
$("#searchText").keyup(function(){
var searchText = $(this).val();
$("ul li").each(function() {
$(this).toggle(contains($(this).text(), searchText));
});
});
Example fiddle

ajax get tha value of input type select option [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
how to get the value of an input select option selected , I have a form with select option. If an option is selected with the value "example_5" I want it to get the value of an selected option
var tow;
var str;
var e = document.getElementById("street");
str= e.value;
Here's the code you need to use:
var e = document.getElementById("street");
var str;
for(var i = 0; i <e.children.length; i++){
var opt = e.children[i];
if (opt.selected == true){
str = opt.value;
}
}

Looking for the Javascript of these JQuery lines of code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm currently learning HTML 5 + javascript, and one of the examples I'm looking at is written in JQuery. I realise that JQuery would be simpler to some people, but as I am just coming to terms with Javascript and no nothing of JQuery, I'm looking to get some lines of code translated from one to the other.
I know what these lines of code does, I'm just looking for it in JavaScript.
var showForm = function() {
if(editMode) {
var transaction = transactions[editMode];
amountField.value = transaction.amount;
if(transaction.type) $('[value='+transaction.type+']').attr('checked', 'true');
if(transaction.cleared) $('#cleared').attr('checked', 'true');
noteField.value = transaction.note;
dateField.value = transaction.date;
$('#time').val(transaction.time);
postcodeField.value = transaction.postcode;
searchField.value = ''
} else {
clearUI();
}
$('#formDiv').show();
$('#toolbar').hide();
$('#tableDiv').hide();
$('#map_div').hide();
$('#sum').hide();
Replace the lines where ever you select the element by ID with the corresponding ID
$('#cleared') ---> document.getElementById('cleared')
you can also use querySelector metod to access the element directly.
var cleared = document.querySelector('#cleared');
To show or hide a element you would need to set the style.display property
// To hide
cleared .style.display = "";
// To show
cleared .style.display = "block";
To get the element based on the attribute would be a bit of javascript..
$('[value='+transaction.type+']')
Where in you would need to iterate over each element and get the attribute of that element and then compare with the value.

Categories

Resources