jQuery cannot get input value [duplicate] - javascript

This question already has answers here:
How to select JSF components using jQuery?
(4 answers)
Closed 7 years ago.
I am trying to get input value after keypress by jQuery. I have tried few ways to get it and I receive undefined or "". Using JSF+RichFaces. Input is the <h:inputTextarea> Trying to get value by:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#myform:idTextarea").keyup(function(){
var testValue = jQuery("myform:idTextarea").val();
console.log(testValue);
});
});
</script>
The variable in log is undefined.
When I add "input:" like that
var testValue = jQuery("input:myform:idTextarea").val();
I get j_id12.
When change like that:
var testValue = jQuery("#myform:idTextarea").val();
I get "".
Have someone any clues?

You need to escape CSS meta-character while buidling the jquery selector:
jQuery("input\\:myform\\:idTextarea").val();
Escaping css meta character in jquery

You don't need to find same element for which keyup is registered, just use this
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#myform:idTextarea").keyup(function(){
var testValue = jQuery(this).val();
console.log(testValue);
});
});
</script>

Related

Datatype .innerHTML [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 2 years ago.
I´m getting a NaN out of this script, is it a data type problem and how do i solve it?
<script>
function calc(){
var profile = document.getElementById("profil");
var hours = document.getElementById("hour");
document.getElementById("result").innerHTML=profile*hours;
}
</script>
Present the result like this
<button onclick="calc()">Calc</button><br>
<p id="result"></p>
You have to get the value from the element.
var profile = document.getElementById("profil").value;
With .getElementById you get a DOM Element.
Supposing "profil" and "hour" are input elements, you want to obtain their values:
<script>
function calc(){
var profile = document.getElementById("profil").value;
var hours = document.getElementById("hour").value;
document.getElementById("result").innerHTML=profile*hours;
}
</script>

Show Parameter Segment of URL using JavaScript [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 5 years ago.
So I am trying to get the last part of a link and show it in a div on the HTML.
I'm using JavaScript and HTML to perform this; here's what I have so far:
<script type="text/javascript">
var pageURL = window.location.href;
var lastURLSegment = pageURL.substr(pageURL.lastIndexOf('/') + 1);
document.getElementById("getLink").innerhtml = lastURLSegment;
</script>
<div id="getLink"></div>
And here is my link:
https://playmafia.000webhostapp.com/world/play/create/?id=m12345
However, after executing this, nothing happens or shows. And plus, this code, as far as I know, will retrieve "?id=m12345".
I am new to javascript so any help would be great in figuring this out. Also, this may need jQuery, I'm not completely sure, so I will accept answers that involve it.
Once again, how can I get the parameter of the URL (in the example, m12345) to display in the HTML under the div with id "getLink"? Thanks to anyone who can help.
Try This :
var pageURL="https://playmafia.000webhostapp.com/world/play/create/?id=m12345";
var lastURLSegment = pageURL.substr(pageURL.lastIndexOf('/') + 1);
var value=lastURLSegment.substr(lastURLSegment.lastIndexOf("=")+1);
document.getElementById("getLink").innerHTML = value;
<div id="getLink"></div>
Try by changing document.getElementById("getLink").innerhtml to document.getElementById("getLink").innerHTML
I hope you need some changes in your code
document.getElementById("getLink").innerHTML = lastURLSegment;
TRY THIS ...

Attribute value as variable name [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 7 years ago.
I'm trying to get a dynamic attribute value to be used as a var name
What I have:
$(this).html(""+$(this).attr("id")+"");
It gives me the value as plain text. What I want is that it uses the attr("id") value as a Var like:
$(this).html(""+myVar+"")
Final example:
<div class="text" id="text1"></div>
<div class="text" id="text2"></div>
var text1 = "hello world"
var text2 = "bye world"
$(document).ready(function () {
$(".text").click(function(){
$(this).html(""+$(this).attr("id")+"");
});
});
Basically I want a dynamic way of calling to certain id's and getting their attribute value to be used as the Var name instead of plain text.
It all depends on variable namespace and scope, but generally next code should do the job.
$(".text").click(function(){
var varFromAttr = window[$(this).attr("id")];
$(this).html(varFromAttr);
});
Question is almost the same as this question.

onChange select list [duplicate]

This question already has answers here:
What is console.log?
(26 answers)
Closed 8 years ago.
<form name="storelocator_cities_form" id="storelocator_cities_form">
<select name="storelocator_city" id="storelocator_city" onchange="storeLocatorForm.fetchStoresByCountryAndPostCode(this)">
<option value=""></option>
</select>
</form>
Hi i want to capture the option value and then compare it to a specific city and i'm stuck. I'am new with both JS and jQuery but i guess that i will need to do something like
jQuery(function(){
$('#storelocator_city').on('change',function(){
var city = $(this).val();
});
});
I would like a way to see that i get the correct value in the var in the console.
I would like a way to see that i get the correct value in the var in
the console
Sure , but first :
Why do you have 2 onchange events ?
Anyway - to see the value in console : try this :
jQuery(function(){
$('#storelocator_city').on('change',function(){
var city = $(this).val();
console.log(city); <------- Try this
});
});

jquery - how to add an variable as an id in jquery command.? [duplicate]

This question already has answers here:
How to use JavaScript variables in jQuery selectors?
(6 answers)
Closed 8 years ago.
Its a simple question, could not find an answer from google.
Code
$.click(function(){
var curID = $(this).parent()[0].id;
$("#"+curID input).attr("checked",true);
});
Onclick function is giving me the parent id, Now, using the parent id i want to find the input element and add checked attribute.
I am not sure of the syntax of querying by dynamic ID.
I want to know how can i query by dynamic variable.
Thanks in advance.
$("#"+curID).find('input').attr("checked", true);
Or
$(this).parent().find('input').attr("checked", true);
Or
$('input', $(this).parent()).find('input').attr("checked", true); // using the scope argument
The selectors are strings... So should be handled as strings by concatenating the variables: and texts
$.click(function(){
var curID = $(this).parent()[0].id;
$("#"+curID+" input").attr("checked",true);
});
Your search is probably too specific. Break tasks down into their components instead of looking for a complete solution to a very specific problem. You are just dealing with basic string concatenation here.
You want:
var selector = "#foo input";
You have foo in a variable.
var selector = "#" + foo_variable + " input";

Categories

Resources