How to remove/change an input value using javascript - javascript

How do I remove the value 'Anonymous' in this input that I have and replace it with a placeholder text 'Your name' using javascript/jquery? I don't have access to the HTML code.
This is what I have so far, but don't really know where to go from there.
document.getElementById('txtYourName').placeholder =' Your Name ';
HTML
<input id="txtYourName" type="text" value="Anonymous" name="txtYourName"></input>

Add this second line here...
A placeholder is only seen when the value happens to be blank. With the code below, you can set the placeholder, as well as erase the default value in your field.
document.getElementById('txtYourName').placeholder =' Your Name ';
document.getElementById('txtYourName').value = "";
Demo: http://jsfiddle.net/723vL/

You can use .val():
$('#txtYourName').val('');
or pure javascript using:
document.getElementById('txtYourName').value = ""
If you want to set new value then just put your value inside "", like:
$('#txtYourName').val('new value');
With jQuery, your final code should look like:
$('#txtYourName').attr('placeholder','Your name');
$('#txtYourName').val('');
Fiddle Demo
With pure JS, you final code should look like:
document.getElementById('txtYourName').placeholder ='Your name';
document.getElementById('txtYourName').value = "";
Fiddle Demo

That should do it, so long as you are running that script either on an onload event, jquery's ready event or at the very bottom of the page, once the DOM has been rendered.
Are you getting an error in your console?

try this
returns value:
$('#txtYourName').attr("value");
sets value
$('#txtYourName').attr("value", "");

use
document.getElementById('txtYourName').value ='some vaue';
if you are using jQuery then you can use
$("#txtYourName").val('some value');

Related

set text of input value with javascript

I know this is probably a piece of cake for all, but im really not any good with javascript.
I would like to set the value of html input with javascript.
I have an input like this:
<input id="input-data" value=""/>
I would like to set the text of the value with javascript, meaning that if id pass value like "CocaCola" to input, it should display "CocaCola" in input
This is how i try
document.getElementById("input-data").value = "CocaCola";
But no data gets displayed in input. When i run debugger and put value as my watch, the "CocaCola" is stored in value.
What on earth am i missing?
Make sure your code is under HTML tag, like this:
<input id="input-data" value=""/>
<script>
document.getElementById("input-data").value = "CocaCola";
</script>
Or you can use:
window.onload = function(){
document.getElementById("input-data").value = "CocaCola";
}
It works any time and anywhere.
Your JavaScript code to set value is right.
You can also try jQuery to set value like this,
$('#input-data').value = 'CocaCola';

Setting content of an element in tinymce textarea

I have a tinymce textarea (TinyMCE 3.5.11) that contains an element like
<span id="lastreplytimeee">...</span>
I need to access and change the content of this element using the span id as selector.
I have tried things like
tinyMCE.DOM.setHTML('lastreplytime', $input.val());
None worked.
Any suggestions?
this line is within .ready:
$().ready(function() {
jQuery('.flexy_datepicker_input').datetimepicker({
lang:'tr',
format:'Y-m-d H:i:s',
dayOfWeekStart:1,
onChangeDateTime:function(dp,$input){
document.getElementById("lastreplytimeee").innerHTML = $input.val();
}
});
});
1.Maybe try it,this code gets TinyMCE content and then replace some elements (TinyMCE 5)
tinymce.get('TinyMceID').setContent(
tinymce.get('TinyMceID').getContent().replace(
$(tinymce.get('TinyMceID').dom.doc.getElementById('lastreplytimeee')).get(0).outerHTML,
`<span id="lastreplytimeee">` + newValue + `</span>`)
)
2.or use this
tinymce.get('TinyMceID').dom.setHTML("lastreplytimeee", "newValue");
document.getElementById("id") gets the reference to that particular tag.
.innerHTML is to say assign value or display values in the HTML at that particular div you are referring to above.
Try to use-
document.getElementById("lastreplytimeee").innerHTML = $input.val();
If you are using jQuery, try this-
$("#lastreplytimeee").html = $input.val();
Hope it helps :) Happy Coding!
Found the solution, tinymce requires its dom utility to access and play with an element inside it, like:
tinyMCE.activeEditor.dom.setHTML(tinyMCE.activeEditor.dom.select('selector'), 'some inner html');

jQuery find and replace text

I have a text contained on a <textarea> like this:
Hi {nom},
We are glad to accept your invitation.
I need it to become
Hi Dolly Parton,
We are glad to accept your invitation.
I don't want to use any plugin, but I have tried developing this simple functionality with no possitive result:
prevContent=$('textarea').val(); //alerts correctly textarea content
content = prevContent.replace('{nom}','Dolly Parton');
In this case, no replacement is made and content has the same value as prevContent.
This is not meant to make a real time replacemet, it has to be replaced before submitting form:
$(document).on('click','.submitMessage', function(){
prevContent=$('textarea').val();
content = prevContent.replace('{nom}','Dolly Parton');
$.post('../actions/newMessage.php',{ms_content:content}).success(
function(data){
alert("yikes");}
);}
});
SOLVED: This was due to a spell mistake on variable. It was sent like {nom1}, not {nom}. Sorry for the inconvenience.
You need use RegExp with g flag, because in your example only first {nom} will be replaced:
var prevContent = $('textarea').val();
var content = prevContent.replace(/\{nom\}/g, 'Dolly Parton');
and to update textarea you can use .val, like so
$('textarea').val(content);
Example
You will just need to assign value of "content" to textarea, as :
$('textarea').val(content);

jQuery: return string as html

I have a "Label" control on my form which is being populated via jquery. My problem however, is I need to return my string with html tags, but my label doesn't seem to recognize html. Here's my control:
<label id="comment"></label>
And here's how I'm working with it in javascript:
var comment;
comment = $("#comment");
comment.text("<b>Please scan an <br/>item!</b>");
In the above code, the html in my string is ignored. Is there any way I can get my form to recognize the returned html tags?
Thanks
Use the html function :
comment.html("<b>Please scan an <br/>item!</b>");
try comment.html("<b>Please scan an <br/>item!</b>");
You could also use document.getElementById("comment").innerHTML = "<b>Please scan an <br/>item!</b>";
comment.html("<b>Please scan an <br/>item!</b>");

Copying INPUT FILE value to INPUT TEXT with click in jQuery

I would like to copy the value of a input:file to input:text. I can do it with plan JavaScript but I will like to know how to do it with jQuery.
----JavaScript
// This what the script would look like with plain JavaScript
// It works fine. I just woulld like to know who to do it with jQuery.
function fakeScript() {
var filepath;
filepath = document.adminForm.tumb.value;
filepath = filepath.substring(filepath.lastIndexOf('\\')+1, filepath.length);
document.adminForm.tumbFake.value = filepath;
}
If you have something that works in "plain Javascript", it'll work with jQuery too : jQUery is just a library, that adds functions -- it doesn't prevent anything from working (or there is some kind of bug in it ^^ )
var fileValue=$("input[type='file']").val();
var inputValue=$("input[type='text']").val(fileValue);
cheers
You probably cannot use the value attribute of a file input with JQuery.
"The value attribute cannot be used with <input type="file">." - http://www.w3schools.com/tags/att_input_value.asp
To get the text value of a file from an <input type="file"/> element, use yourFileInputElement.files[0].getAsText("utf-8").

Categories

Resources