How can I utilize selectionStart? [duplicate] - javascript

This question already has answers here:
selectionStart-End with textareas
(2 answers)
Closed 10 years ago.
As I understood, selectionStart must return start position from selected text in input text or textarea elements
I have this js code
$("#inpt").on("mouseup" , function () {
alert( $("#inpt").selectionStart);
});
and html
<input id="inpt" type="text" value="bla bla bla" />
When I select some part in text "bla bla bla" the result is "undefined". Yell please, where did I go wrong ?

Try this.selectionStart, it's not the property of jQuery object, but the HTMLInputElement's property.
$("#inpt").on("mouseup" , function () {
console.log(this.selectionStart);
});

Related

value was changed by using "<script>" but it is not shown on the page [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 7 months ago.
<script>
var idn_text = "123"
document.getElementById("idn_id").value = idn_text
</script>
<p><input type="text" placeholder="Results" name="idn" id="idn_id"></p>
the webpage does not display this value, it only displays it as an empty form. How to fix it?
Your input field has not rendered and the script is looking for an element with it's id. A simple solution is to move your script to end of the html file. like this:
<p><input type="text" placeholder="Results" name="idn" id="idn_id"></p>
<script>
var idn_text = "123"
document.getElementById("idn_id").value = idn_text
</script>
Switch the order of your elements and call the script afterwards.
<p><input type="text" placeholder="Results" name="idn" id="idn_id"></p>
<script>
var idn_text = "123"
document.getElementById("idn_id").value = idn_text
</script>

jQuery: Remove string that has no parent element [duplicate]

This question already has answers here:
Remove text with jQuery
(4 answers)
How do I remove text from a node?
(2 answers)
Closed 11 months ago.
I have a document that I can't edit in PHP looking like this:
<div class="car-search-field-div">
<label class="simple_hide">Türen</label>
<div class="myClear"></div>
doors_count
<input id="car-search-form-field-doors_count" name="search[doors_count]" type="text" value="" placeholder="Anzahl Türen ab">
</div>
Now I am trying to remove the string "doors_count" via jQuery but can't adress it properly. When I try something like:
$('.car-search-field-div').remove('iventory_number', '');
The placeholder of the input field gets removed, but the string "doors_number" still is there. I also thought of using the xpath of the string, but that doesn't work either.
Is there a way to adress / remove a string that has no element wrapped around it? Thank you very much in advance!
you can remove inside all text inside .car-search-field-div
$('.car-search-field-div').contents().filter(function () {
return this.nodeType === 3;
}).remove();
more detail enter link description here
demo
You need DOM access
document.querySelector(".car-search-field-div").childNodes.forEach(node => {
if (node.nodeType === 3 && node.textContent.trim() === "doors_count") node.remove()
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="car-search-field-div">
<label class="simple_hide">Türen</label>
<div class="myClear"></div>
doors_count
<input id="car-search-form-field-doors_count" name="search[doors_count]" type="text" value="" placeholder="Anzahl Türen ab">
</div>

Javascript NaN function not working [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 6 years ago.
I am trying to validate a text box to enter only numbers and not alphabets using javascript NaN function.
But i am not getting the correct output
<html>
<body>
<input type="text" id="demo">
<button onclick="myFunction();">Try it</button>
<script>
function myFunction()
{
var x =document.getElementById("demo");
if (isNaN(x))
{
alert("hi");
}
else
{
alert("world");
}
}
</script>
</body>
</html>
x in your code will be an HTMLInput object. It will never be a number.
You want to test the value of parseInt(x.value).

How to replace strike tag to input type text? [duplicate]

This question already has answers here:
Using jQuery to replace one tag with another
(13 answers)
Closed 9 years ago.
How to replacement strike element to input type text?
I have no idea at this time if any kind of solution please tell me here!
HTML Text :
In the <b>joy</b> of <strike>other</strike>, lies our <strike>own</strike>.
replace Text :
In the <b>joy</b> of <input type="text">, lies our <input type="text">.
If I understand your question well, you want this?
$('strike').each(function(){
$(this).replaceWith("<input type=\"text\" value=\"" + $(this).text() + "\" />");
});
jsfiddle: http://jsfiddle.net/M88u4/
Just add following jQuery statement
// search all strike element and replace it
$('strike').replaceWith(function(){
//create input element
var input = $('<input>',{
type: 'text',
value: $(this).html()
});
//replace with input element
return input;
});
Try this:
HTML
<div id="test">
In the <b>joy</b> of <strike>other</strike>, lies our <strike>own</strike>.
</div>
Javascript
$(function(){
var strikes = $('#test').children('strike');
var inputText = "<input type='text' />";
strikes.each(function(){
$(this).replaceWith(inputText);
});
});

Unable to Limit the user input in JavaScript [duplicate]

This question already has answers here:
How to impose maxlength on textArea in HTML using JavaScript
(16 answers)
Closed 8 years ago.
I tried limiting the user input but it wasn't successful, please guide me where I am making mistake.
JS
<script type="text/javascript">
function countLength() {
var maxLength=10;
var length = document.getElementById("txt").value.length;
if(length>10) {
return false;
}
}
</script>
HTML code
<form name="formA" id="formA" action="#" >
<textarea id="txt" name="txt" onkeyup="countLength()"></textarea>
</form>
Your code basically replicates the maxlength attribute, which seems to work (and I don't think is being deprecated?). Just use that.
<input type='text' name='mytext' maxlength='10'>
return false on onkeyup does nothing (as you've probably noticed). I've seen solutions where someone would just alter the value of the textarea, perform a substring operation, and assign that new value back.
Try this:
function countLength() {
var maxLength=10;
var ta = document.getElementById("txt");
var length = ta.value.length;
if(length>maxLength) {
ta.value = ta.value.substr(0, maxLength);
return false;
}
}

Categories

Resources