jsfiddle
http://jsfiddle.net/m3g6rjok/1/
html
<textarea id="feedback" rows="4" cols="50" name="comment" maxlength="50">Enter text here...</textarea>
<br>
<input type="text" value="10" id="lenbox"></input>
js
$(document).ready(function(){
$( "#feedback" )
.keyup(function() {
var value = $( this ).val();
alert(value)
$( "#lenbox" ).val( value );
})
.keyup();
})
I need to calculate the number of words thats entered on the textarea and need to display it on the text box below..I'm using the keyup event to check but it gets triggered even on the page load. How can I fix it
Remove the .keyup(); from your code. This triggers the keyup event on page load.
The keyup event is raised because you trigger one. Remove the .keyup() after your handler function declaration:
$("#feedback").keyup(function() {
var value = $(this).val();
alert(value)
$("#lenbox").val(value);
})
Because you have .keyup(); at the end of your JQuery statement.
Just remove it
You should not trigger the keyUp again. Also, you should move the code that's updating the value outside of the keyUp function, so you can also trigger the code on page load. After you've done that, it's just taking a string, split on spaces and count the words:
var calculateCount = function() {
var value = $('#feedback').val();
var count = value.split(" ").length;
$( "#lenbox" ).val( count );
};
$(document).ready(function(){
$("#feedback").keyup(calculateCount);
calculateCount();
})
http://jsfiddle.net/qd6aznwa/
DEMO
First, Remove .keyup() as has been pointed out already
Then, use input instead of keyup so that I can also catch pasted text.
Finally, count the number of words by using the split method and the length property.
$(document).ready(function(){
$( "#feedback" ).on('input',function() {
$( "#lenbox" ).val( this.value.split(' ').length );
});
});
If you dont mind using HTML5 use the placeholder property
<textarea id="feedback" rows="4" cols="50" name="comment" maxlength="50" placeholder="Enter text here..."></textarea>
Related
How to make the variable "apry" be equal to the written data in "textarea",
So then i will can get its value into URL?
HTML:
<textarea id="post" type="text"></textarea>
<a onclick="location.href = 'http://localhost/arany/?i=' + apry + '';">Reload</a>
JS:
$(document).ready(function() {
$('#post').keyup(function() {
var apry = document.getElementById('post').value;
});
})
You are actually setting the value of apry, but the problem is you then aren't doing anything with it, including not updating your DOM element. You would need the following instead :
$(document).on("keyup", "#post", function() {
$("#theLink").attr("href", "http://localhost/arany/?i=" + $("#post").val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="post" type="text"></textarea>
<a id="theLink" href="#">Reload</a>
Theres no need for a global variable in this case. You can just listen for the click event on the reload button:
<textarea
id="post" type="text"></textarea>
<button id="reload">
Reload
$(document).ready( function() {
$('#reload').click(function() {
location.href = 'http://localhost/arany/?i=' +
$('#post').val();
});
});
http://jsfiddle.net/4j0fohr5/1/
To do what you require it would make more sense to invert the logic. Instead of creating and updating a variable which has the value of the textarea as it's typed in to, simply have an event handler which reads the value from the textarea when the a is clicked. This has the benefit of avoiding an unnecessary global variable. Try this:
<textarea id="post" type="text"></textarea>
Reload
$(document).ready(function() {
$('#reload').click(function() {
location.assign('/arany/?i=' + $('#post').val());
});
});
One thing to note here is that you will have to be careful with line breaks in the value.
Alternatively if you did want to update the href of the a as the textarea is typed in to you could use prop() to do that inside an input event handler:
<textarea id="post" type="text"></textarea>
Reload
$(document).ready(function() {
$('#post').on('input', function() {
$('#reload').prop('href', '/arany/?i=' + $(this).val());
});
});
I see that you want to add the textarea content as a query string "i" parameter when you click the "Reload" button.
For that purpose you only need an input text field instead of a textarea, since the URL does not support line break characters.
Also, you don't need to update the "i" every time you change the text, realize that you need that value just when you click the "Reaload" button.
So, here is what I propose to you to solve your problem:
<input type="text" id="post"></textarea>
<a onclick="goToLocation();">Reload</a>
function goToLocation(){
apry = window.document.getElementById('post').text();
window.location.href = 'http://localhost/arany/?i=' + apry;
}
var apry = null;
$(document).ready(function() {
$('#post').keyup(function() {
apry = document.getElementById('post').value;
});
})
This should work for you
I'm trying to fill three fields with the same text but I'm only writing into the first one. This code is only working once and then it isn't (the alert is working fine constantly).
$( 'textarea[name="posting"]' ).on( "keyup", function() {
var text = $('textarea[name="posting"]').val();
$('textarea[name="posting_twitter"]').replaceWith(text);
alert(text)
});
See the working jsfiddle:
JS:
$('#first').on('keyup', function() {
$('#second').val($(this).val());
});
HTML:
<input id="first" type="text">
<input id="second" type="text">
All you need to do is use jQuery's .val() method, which can both set and get the value of an input element. Read the documentation.
Try this:
$( 'textarea[name="posting"]' ).on( "keyup", function() {
var text = $('textarea[name="posting"]').val();
$('textarea[name="posting_twitter"]').val(text);
alert(text);
});
Text area value can be replaced with val()
$('textarea[name="posting_twitter"]').val(text)
like this?
html
<textarea name = "posting"></textarea>
<textarea name = "posting_twitter"></textarea>
jquery
$('textarea[name="posting"]').on("keyup", function(){
var text = $(this).val();
$('textarea[name="posting_twitter"]').val(text);
});
I have done a validation to avoid special characters with the following code for all input text, however, I have input text that require having special characters. My code is as follows
$('.twTextinput input, .twTextinput textarea').not( $( '#txtEmailPersonal input, #txtEmailTrabajo input' )).keyup(function (){
this.value = this.value.replace(/[^a-zA-Z0-9 _]/g,'');
});
The problem is this, I have an input text that must be within the selector .not(). Is a input text that has an id that starts with "iccw". I tried with this code but does not work
$('.twTextinput input, .twTextinput textarea').not( $( '#txtEmailPersonal input, #txtEmailTrabajo input, input[id^="iccw"]' )).keyup(function (){
this.value = this.value.replace(/[^a-zA-Z0-9 _]/g,'');
});
Any suggestions.
Thanks in advance.
That is an awfully complex way single out one special input, don't you think?
I would tend to want find all "regular" textareas or inputs into an container and use find to collect them, and then single out the other "normal" inputs.
$normal_input_list = $(container).find( 'input.normal, textarea.normal' );
$special_input_list = $( '#icww' );
onKeyupNormal = function (){
this.value = this.value.replace(/[^a-zA-Z0-9 _]/g,'');
};
// and now bind the handler
$normal_input_list.on( 'keyup', onKeyupNormal );
If there is a good reason to select as you have, then I have determined the following should work:
$('input, textarea').not( '#wmd-input' );
Specifying input and ID is redundant, as ID is already unique across the page.
Hope that helps!
Here's a hack:
$('.twTextinput input, .twTextinput textarea').not( $( '#txtEmailPersonal input, #txtEmailTrabajo input'
)).keyup(function (){
if( this.id != 'iccw' ) {
this.value = this.value.replace(/[^a-zA-Z0-9 _]/g,'');
}
});
I would tend to control this with HTML attributes. I find it more manageable.
<textarea data-allowedchars="[^a-zA-Z0-9 _]"><textarea>
<textarea ></textarea>
<input data-allowedchars="[^a-zA-Z0-9 _]" />
<input />
JS
$("body").on("keyup", "[data-allowedchars]",
function(e){
var $t = $(this),
re = new RegExp($t.data("allowedchars"),'g');
$t.val($t.val().replace(re, ""));
}
);
Sorry this doesn't directly answer your question. It is hard to see what is wrong with your syntax without seeing the code.
I have my markup like this
<div class="text-input-area">
<input type="text" id="text-input"/> <br />
<input id="button" type="submit" value="Preview" />
</div><!--.text-input-area-->
In the input area I will type only text. Now when I will click on button Preview it should show the text that I had type. For that I have made my jquery like this
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#button').click(function() {
var textvalue = $( "#text-input").text();
alert('textvalue');
});
});
</script>
But its not working at all. Can someone tell me how to do this?
demo http://jsfiddle.net/gmeEL/
API:
.val() : http://api.jquery.com/val/
Hope this will fit the cause :)
code
jQuery(document).ready(function() {
jQuery('#button').click(function() {
var textvalue = $( "#text-input").val();
alert(textvalue);
});
});
Use $( "#text-input").val() instead of $( "#text-input").text() and alert(textvalue) instead of alert('textvalue')
Complete Code:
jQuery(document).ready(function() {
jQuery('#button').click(function() {
var textvalue = $( "#text-input").val();
alert(textvalue);
});
});
Fiddle Demo
Two problems
need to use .val() to get the value of an input field
need to use the variable textvalue for the alert not a string literal 'textvalue'
Try
jQuery(function ($) {
$('#button').click(function () {
var textvalue = $("#text-input").val();
alert(textvalue);
});
});
for inputs and select use $(selector).val()
for span use $(selector).text()
It should be a val() i.e. a jquery function that returns a value of the element
var textvalue = $( "#text-input").val();
Please note that
The .text() method cannot be used on form inputs or scripts. To set or
get the text value of input or textarea elements, use the .val()
method. To get the value of a script element, use the .html() method.
http://api.jquery.com/text/
In my page I have two radios, user may select any one and type their search word in the input box, on button click I would like to append to their search keyword the value of selected radio?How do we do that in jquery or javascript?
So if they typed in Dance and selcted 'classes' the actual search keyword being passed will be 'Dance classes'
<div>
Events<input name="EventRadio" type="radio" value="Events"/>
Classes<input name="EventRadio" type="radio" value="Classes"/><br /><br />
<input type="text" name="T1" style="width: 302px"/>
<input type="button" value="Search" onclick="javascript:_SFSUBMIT_"/>
</div>
Need some more information: are you wanting the additional keyword to be displayed in the text box as well?
All you really need to do is capture the value attr of the radio if checked in your SFSUBMIT function. jQuery has several different constructs to do this.
If I'm understanding the question, when the user clicks the button, append what's in the textbox with the radio button currently checked?
$("input[type='button']").click(function() {
var text = $("input[name='EventRadio']:checked").val();
if (text) {
var $tb = $("input[name='T1']");
$tb.val($tb.val() + text);
}
});
Of course this will fire for all buttons - naturally you can add an id to this particular button an narrow the selector
$("#buttonId").click(function() {
$( "button" ).live(
'click',
function ( )
{
$strVal = $( "[name='T1']" ).val( ) . $( "[name='EventRadio']:checked" ).val( );
$( "[name='T1']" ).val( $strVal );
}
);
$('input[type="button"]').on('click', function () {
var $this = $(this),
concocted = $this.prev().val() + ' ' + $this.prevAll('[type="radio"]:checked').val();
//you can now do what you want with the `concocted` variable, which is in the format "{text-input} {radio input}"
});
Here is a demo: http://jsfiddle.net/LAuZW/
Note that this code will work if you have more than one set of inputs since all the selectors are based off this which is the clicked button input.
Also note that .on() is new in jQuery 1.7 and in this use-case is the same as .bind().