How to prevent user from deleting text in a text box - html

Ok so I am new here and was wondering if someone a little more advance then me can help me out.
I have text box on my website with code for user(s) to copy the code that's in the text box and paste the code in Orkut scrapbook which will generate a imagine.
I am using onclick so when user clicks on code it highlight it and then they can copy.
The problems is that you can delete or remove text from within box, if your not careful.
I DONT want the user to be able to delete code in text box. How can I prevent this from happening without removing the onclick scrip.
Please if you could when reply maybe include the sample code above and highlight the new added code so I can see where to make my changes.
I hope I explained this well for anyone to understand!

You can add readonly="readonly" to your textbox tag. Example:
<input type="text" name="someNAme" readonly="readonly" >
you can also try with:
<input type="text" name="someNAme" disabled="disabled" >

Here is a totally editable textbox, and a not editable textbox. The difference is that in the second textbox, there is a readonly attribute, which prevents the user from editing the text. (Run the code snippet!)
function copy(what) {
var copyText = document.getElementById(what);
copyText.select();
copyText.setSelectionRange(0, 99999)
document.execCommand("copy");
}
<textarea id="selectable">This is totally editable!</textarea>
<button onclick="copy('selectable')">Copy this !</button>
<br/>
<br/>
<textarea id="unselectable" readonly>This is not editable!</textarea>
<button onclick="copy('unselectable')">Copy this !</button>

Related

How to get onclick button to show text in input box

I was having this problem with a more complex chunk of code, so I started messing with a html "joke" with the movie The Seven, and realized it was the same problem. Why doesn't it do anything when I click the button? I've tried it adding a function and script as well, get same problem. I want it to show the text inside the (formerly blank) input box when you click the button.
<html>
<body>
The Box:<input type="text" id="thebox" value="" size=10>
<br><input type="button" value="What's in the booooox?" onclick="document.getElementById('thebox').innerHTML='head of gwyneth paltrow';">
</body>
</html>
innerHTML, as the name suggests, refers to the HTML content of an element. You need to use value
The Box:<input type="text" id="thebox" value="" size=10>
<br><input type="button" value="What's in the booooox?" onclick="document.getElementById('thebox').value='head of gwyneth paltrow';">
See it in action

Input readonly backspace issue

In my input element, when I navigate to the element and enter the backspace key using keyboard, it navigates to the previous page, when I set the input is readonly.
My code is below. Please share your knowledge.
<div class="div1">
<label class="div1" for="inputfor">cash:</label>
<input type="text" id="cashinput" readonly="readonly" />
</div>
I know that this question was asked 2 years back. Since I have a solution that worked for me, I am tempted to share it with everyone.
The fix is quite simple:
<input type="text" onkeydown="event.preventDefault()" readonly="readonly"/>
The event.preventDefault() will stop the backspace from navigating away from the page and you can also select and copy the text.
Thanks.
You could always just keep the input field readonly disallow input altogether.
<div class="div1">
<label class="div1" for="inputfor">cash:</label>
<input type="text" id="cashinput" onkeydown="return false;" readonly="readonly"/>
</div>​​​​​​​​​​​​​​​
This is expected and correct behavior for an input with a readonly attribute.
An input with this attribute cannot be modified, it can only be viewed, you can access the content by tabbing and copying, but that is it.
Remove the readonly attribute if it's not needed.
This line helped me:
onkeydown="if(this.readOnly) event.preventDefault();"
if you are in page because of location.href = "new/page/url";
use location.replace("new/page/url") instead.
location.href = "new/page/url"; // loads new page and saves current page as history in IE
refer
https://developer.mozilla.org/en-US/docs/Web/API/Location

Clear text field in a form and resubmit it

I have a form in html (generated in php), which contains also a text field.
To submit the form I use a submit button and the results appear on the left side of the page. Now, I would like to add another button which would clean the text in the text field
and resubmit the form (with value="" in the text field). Do you have any idea how to do it?
Simple <input type="reset"> does not submit the form.
your button should be like this:
<input type="button" onclick="DeleteText();" value="Delete text" />
and your javascript code:
function DeleteText() {
document.getElementById('my_text_input_id').value = '';
document.getElementById('my_form_id').submit();
}
suppose the text box has id="myText" and the new button has id="clearBtn", and the form has id=myForm. You can do the following:
$(#clearBtn).on("click",function() {
$('#myText').val("");
document.forms["myForm"].submit();
})
This is untested, but proposes an idea. Also, This is a jquery solution, which I recommend over pure javascript in most cases.
HTML5 brings us the "placeholder" attribute, which allows for a Pure CSS implementation of this.
<input type="text" name="focus" required class="search-box" placeholder="Enter search term" />
I have successfully used Shidhin's solution, found here:
http://codepen.io/shidhincr/pen/ICLBD

Why does adding a hidden text box to a form stop IE refreshing on enter

I was looking for a fix to stop IE refreshing the page instead of submitting my single line form, when a user hits enter instead of clicking go.
I found this solution, which works well, but I was wondering if anyone could explain why it works?
The solution I used is to add a hidden text input within the form tags, like this
`<form name="SearchForm" id="SearchForm" method="get" action="">
/*This is the hidden text input*/
<input type="text" style="visibility:hidden;display:none;" name="ieSearchEnter">
</input>
<fieldset>
<span><input type="text" name="Search" id="Search"/></span>
<div class="field actions">
<input type="submit" name="Go" id="Go" class="submit" value="Go"/>
</div>
</fieldset>
</form>`
which i found here.
Thanks!
Are you really setting the ACTION value to an empty string, or did you just do that for your code sample?
I don't think IE is really "refreshing the page"-- I think it's automatically submitting your form.
Here's a simple test page: http://www.enhanceie.com/sandbox/simpleform.asp. When you hit enter, you'll see that the URL is updated to pass the user's value.
IIRC, there is code in IE's form-handling that says that if you have form containing a single test field, then hitting ENTER will submit that form. In your workaround, you've added an additional text field so that optimization is not applied.
I think maybe your server-side code is REQUIRING that the form submission contains "Go=Go" or it ignores the submitted value (Search=Whatevertheuserhadtyped) and simply re-displays the form. If you change the server-side script such that it does not require Go=Go, then your problem should go away.

How to remove 'submit query' from a form submit?

I have an html form and the submit button says "submit query". How can I remove this text? I am using a background image for the submit button and this text is messing up the button :( Any help would be greatly appreciated.
If you do not give your submit button a value
<input type="submit" />
instead of something like
<input type="submit" value="Submit" />
it will display 'submit query' by default. Try giving it a space as the value.
use:
<input type='submit' name='btnTest2' value=''>
Leave the value blank and there will be no words on the button. Since you're using a background image a for the button, give the button a height and width, otherwise it will display as a small gray blip (because there are no words on the button).
Background images are not content. If you want to use an image to tell people what a submit button will do, use a real image. As a bonus that allows you to provide alternative text for users who cannot see the image (e.g. because it failed to load or because they are blind).
<button type="submit">
<img src="example.png" alt="Submit">
</button>
Just use the value attribute as shown below:
<input value="Whateveryouwant" type="submit">
You just have to give it a value:
<input type='submit' name='btnTest'>
<input type='submit' name='btnTest2' value='Push Me'>
In the example above, btnTest renders as "Submit Query" while btnTest2 renders as "Push Me". Hope this helps.
UPDATE: You can do this to not display any text.
<input type='submit' name='btnTest2' value='' style="width:100px;">
Not sure if this was relevant then, but we would use type="image" rather than type="submit"
Just put a space between the value quotes. Simple fix.
Read the question before you reply. You may actually help someone.
Unfortunately, this does not work, at least not in a CMS. I've tried the space and the but IE8 will not recognize it. If I put the same in the value, it reverts back to 'Submit Query'. Just updating for anyone else who finds this method through a search.
EDIT : I added text indent: -9999px; to my CSS, and it seems that it worked. I still added the space in the value attribute for good measure.
I had this issue as well. If you don't set a value for a submit button it defaults to "Submit Query". I assume you are using an image for your submit button since you have the default value.
If you want to fix it for IE8 add a text indent using CSS which will push the default value off the screen.
text-indent:9999px;
If you want to fix it for IE9 you also need to change the default value because the text-indent doesn't work :( in your submit button add the following:
value=" "
I found this to work without a non-breaking space and tested it to my satisfaction on the IE's on browserstack. If you want to use the breaking space, feel free; I'll include the code.
value=" "
Also, thank you to the other stack-responders, you helped me fix this issue on IE9.
Also you inspired me to post my findings here and possibly help others!
If you are using something like a jQueryUI dialog button then you do not want to have the input button show up in the form, but rather just have it in the footer of the dialog. I accomplished this by doing the following:
Because IE will automatically put in an <input type="submit" /> I put this in the form instead: <input type="submit" style="display:none" />
Then later in the dialog JavaScript I put:
$("#register-dialog").dialog({
title: "Register",
modal: true,
width: 700,
buttons: {
Register: function () {
$('#registrationForm').submit();
},
Close: function () {
$(this).dialog("close");
}
}
});
Just remove the text with jQuery?
jQuery:
$('#btnSubmit').val('');
Plus the solutions mentioned with HTML and jQuery, you can put
font-size: 0px;
for the input and it wouldn't show the text anymore.
This worked in my case.
that's it browser show default use this
value="submit" is important
use attribute value="submit"
nothing just do this
<input type="submit /"
a slash with a space and u will not see the Submit or Submit Query no need to give value