How to get textarea value into a varaible outside function - javascript

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

Related

Check if input value is not the same onBlur

How can I check if the value of an input box is not the same after blur?
$("#username").on('blur', function() {
$("#usertext").append("new input<br>");
})
Check this jsFiddle: https://jsfiddle.net/xztptsdg/
Let's think that I enter "Josh" in input and after blur, will append new input box. But, user can "re-blur" the username input, and will append other input.
I want to check if the value is the same, not append new input.
You may use change instead of blur, for example:
$("#username").on('change', function() {
$("#usertext").append("new input<br>");
});
So, there is no need to check if the value changed or not because the change event is sent to an element when its value changes.
See this fiddle
You can keep a global variable to store the current value of the textbox and then check whether the entered value is the same as the previous one. If not, then append the new input text and also set the global variable with the new one. Below is the Javascript that does this.
JS
var txt = "";
$("#username").on('blur', function() {
if (txt != this.value) {
$("#usertext").append("new input<br>");
txt = this.value;
}
})
I would suggest you to use change(). According to the docs
The change event is sent to an element when its value changes.
See the fiddle and below is the JS code with change().
$("#username").change(function() {
$("#usertext").append("new input<br>");
});
You can do it like following snippet.
var text = '';
$("#username").on('blur', function() {
if(this.value != text){
text = this.value;
$("#usertext").append('new input<br>');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="username" id="username">
<br>
<span id="usertext"></span>

How to show JavaScript variable in colorbox

I use colorbox jquery and have problem to show variable in colorbox.
I have variable called wp_store_caption that get value from input type :-
<input type="text" id="title" class="ab_form_text wp_store_caption require" name="wp_store_caption" value="">
Now i use colorbox like :-
jQuery(document).ready(function() {
var wp_store_caption = jQuery('#title').val();
jQuery(".open-popup-link").colorbox({html:"<h1>"+wp_store_caption+"</h1>"});
});
But canot show value of wp_store_caption, But when use alert() without colorbox, I can see the value.
Where is problem ?!
It is not happening because when you write
jQuery(".open-popup-link").colorbox({html:"<h1>"+wp_store_caption+"</h1>"})
you bind the value of wp_store_caption, which is initially not defined.
You need to bind click event and assign value to wp_store_caption, and then call colorbox function.
You should write this:
$(".open-popup-link").click(function () {
$.colorbox({
html: "<h1>" + $('#title').val() + "</h1>"
});
});
See DEMO here.
In this example, I have predefined value of title. Please note this value will not update the heading in colorbox because the value of -wp_store_caption is not being updated.
You can bind to .blur() to get it work as here.
$(function() {
$('#title').blur(function(){
var wp_store_caption = $('#title').val();
if (wp_store_caption.length > 0)
$.colorbox({html:"<p>" + wp_store_caption + "</p>"})
});
});

How can I add to the end of the string that already exists in input field?

Now, If I hit the button, it clears all in the input field, and it automatically inputs "#marry" to it.
But, I don't want it to be cleared:(
What if I want to add "#marry" to the end of the strings that already exists in the input field?
How can I customize my javascript part?
Input field
<textarea class="box text_area" cols="10" id="input" name="comment[body]"></textarea>
button
<a href="#topic" id="username" value="#marryā€¯><span class='btn'>reply</span></a>
javascript
$(document).on('click', 'a#username', function() {
$(".box#input").val($(this).attr('value'));
}
val() has a callback with the arguments index and value, you can use that to easily add something to the value.
$(".box#input").val(function(_, val) {
return this.value + 'some extra string';
});
$(document).on('click', 'a#username', function() {
var self = this;
$(".box#input").val(function(_, val) {
return val + self.value;
});
});
First of all adeneo's answer is good and you should read it. Here is an alternative solution that does not use jQuery:
I assume that both these elements are a part of a form. Let's say for instance the form has an ID of "post". We can access it using document.forms and then its fields as such:
var input = document.forms.post["comment[body]"];
Now, we can add to its value whenever the button is clicked. First select username with getElementById or querySelector and then add the event:
username.addEventListener("click", function(ev){
input.value += ev.target.value;
});
Or with jQuery (this also delegates if the element is not in the DOM yet):
$(document).on('click', 'a#username', function() {
input.value += this.value;
});
It might be desirable to append an extra space between the current text and the username.
append #marry at the end of text area. you can use bellow code its working fine.
$(document).on('click', 'a#username', function () {
var txtvalue = $(".box#input").val();
$(".box#input").val(txtvalue + $(this).attr('value'));
});
see jsfiddle link http://jsfiddle.net/F6mkh/1/

jQuery .val() not setting the value of <select>

I would like to know why jQuery's .val() function is not setting the value of the <select> control for me after I called replaceWith, but it is working otherwise.
Please see here for a (not) working example.
<select><option>ABC</option><option>DEF</option></select>
<input type="button" onclick="ControlOff()" value="Turn Off Control" />
<input type="button" onclick="ControlOn()" value="Turn On Control" />
<input type="button" onclick="Test()" value="Value Setting Test" />
function ControlOff() {
$('select').each(function () {
$(this).replaceWith('<span class="select-type">' + $(this).val() + '</span>');
});
}
function ControlOn() {
$('.select-type').each(function () {
var selected = $(this).text();
$(this).replaceWith('<select><option>ABC</option><option>DEF</option></select>');
$(this).val(selected);
});
}
function Test() {
$('select').val('DEF');
}
The problem is, that $(this) in $(this).val(selected) refers to the removed <span> element, not your new element. You need to replace it with:
$('select').val(selected);
to grab the previously inserted new element.
Also, your code is unecessarily complex, this does the same thing, but simpler:
function ControlOn() {
$selectText = $('.select-type');
var selected = $selectText.text();
$selectText.replaceWith('<select><option>ABC</option><option>DEF</option></select>');
$('select').val(selected); // Use an id instead to match: #my-select-id
}
Make sure to give the <select> element an ID, otherwise it's going to mess up once you introduce a new <select> element somewhere else on the page.
See here for a working example.
The problem is that in ControlOn you have an each which is looping over .select-type elements which are span's and spans cannot be set with the val method:
You can fix this by changing the method to this:
function ControlOn() {
$('.select-type').each(function () {
var selected = $(this).text();
var $select = $('<select><option>ABC</option><option>DEF</option></select>');
$(this).replaceWith($select)
$select.val(selected);
});
}
Live example: http://jsfiddle.net/qSYYc/4/
set value of options will solve your problem. jsfiddle
<select><option value='ABC'>ABC</option><option value="DEF">DEF</option></select>
function ControlOn() {
$('.select-type').each(function () {
var selected = $(this).text();
$(this).replaceWith($('<select><option>ABC</option><option>DEF</option></select>').val(selected));
});
}
Rewrite your code like above, it would work!
The element referenced by this won't change to the select element you just created, it will always be the span element inside the scope of that function. Therefore you should set the value to the newly created select instead of the invariant $(this)!
I'd suggest you to use "disabled" attribute to turn select on and off, it, won't mess up the .val() functionality
function ControlOff() {
$("select").attr("disabled", "disabled");
}
function ControlOn() {
$("select").removeAttr("disabled");
}

Knowing the last clicked textbox via Javascript

I have a form and a button.
I need that when I click on a textfield, and then click this particular button, the textbox which was clicked last will change its value to say "BUTTON HAS BEEN CLICKED".
Is there a way via JavaScript how I can know the last textbox which was clicked?
Many thanks in advance.
You need to store a reference to the text box when you click it. The easiest way to do that is to create a global variable for the reference. Then you would update the reference with the textbox's onclick event. Here is an example:
HTML:
<input id="myTextBox" type="text" onclick="updateCurText(this);">
<input type="button" value="click me" onclick="updateText();">
JavaScript:
var currentTextBox = '';
function updateCurText(ele) {
currentTextBox = ele.id;
}
function updateText() {
document.getElementById(currentTextBox).value = 'BUTTON HAS BEEN CLICKED';
}
Live example.
jsumners is correct, however I would probably recommend avoiding global variables, and if you're using something like jQuery you have encapsulate a lot of the logic in a single file:
$(function() {
var lastBox = false, formSelector = "form.myClass";
// Change events
$(formSelector + " input[type='text']").click(function() {
lastBox = this;
});
// Button click
$(formSelector + " button").click(function() {
if (lastBox)
$(lastBox).val("BUTTON HAS BEEN CLICKED");
});
});
live

Categories