`innerHTML` not working with `input` tag but `value `does - html

I am learning the MEAN stack. I'm performing CRUD operations. I'm stuck with update. Not with the operation exactly. Actually before updating a previously posted article I want to load contents in text fields for final changes. Those text fields are title and content. Where title is a simple text field and content is quill text editor value.
Here is my articles.component.html
Title:<br>
<input type="text" id="title-container" name="title" >
<br>
Content:<br>
<input type="text" id="content-container" name="content">
<br><br>
<input type="submit" value="Submit">
and here's my article.component.ts
This method is called from somewhere else when I click edit button.
onPress2(id) {
this._articleService.fetchArticle(id)
.subscribe (
data => {
document.querySelector('#title-container').innerHTML=data.title;
document.querySelector('#content-container').innerHTML=data.content;
}
);
}
Everything works perfectly if I replace innerHTML with value. But I cannot do so because my content field has value something like <h1>How to make Cheese cake</h1><br>... because I am using quill text editor. Please tell me what wrong with this code.

I think all you looking for is
document.querySelector('#title-container').value=data.title;
document.querySelector('#content-container').value=data.content;

I did some more research on html elements. Here are my observations and solution.
You cannot use innerHTML on input tag because it is a self closing tag and innerHTML as the name suggests works with tags which have both opening and closing tags For eg. div, 'span', etc. So, innerHTML and value are two different things.
Solution
Instead of an input tag, you can use div and add an attribute contentEditable="true" which will make it editable like input field:
<div contentEditable="true" id="content-container" value="">
</div>
This will solve the problem. And in ts file. You are good to use:
...
document.querySelector('#content-container').innerHTML=data.content;
...

Related

How To Retrieve the Value of a textarea

I'm trying to access the text inside of a text area using cheerio , but its text is not shown in the HTML Dom .
this is the HTML of the text area:
<textarea id="MainContentPlaceHolder_txtGtin-inputEl" name="MainContentPlaceHolder_txtGtin" rows="4" cols="20" readonly="readonly" class="x-form-field x-form-text x-form-textarea" autocomplete="off" aria-invalid="false" data-errorqtip="" style="width: 100%;"></textarea>
and this is a picture of the text area in the browser:
i guess its some sort of server generated text that doesn't show up in the Dom ( is it? ).
how can i get the text from the HTML of the page and inside the code?
I'm Using Puppeteer + cheerio to scrape the page , if you know any way to help me with this in either one id be thankful .
It's possible the text content is being added by JavaScript after the page has loaded. You may need to wait a second or two after the loaded event is detected by Puppeteer before you fetch the content.
Also, check whether that text is added using a placeholder attribute rather than standard DOM content.

Create/Place an anchor <A HREF> within an <INPUT> field

Is there any way to allow a link/anchor within an input field so that whatever text is in the field is ALSO clickable and actionable?
This is unfortunately not possible in HTML 4 or below. Even with HTML5 which has several new INPUT TYPEs, including URL, it only does validation and has some other useful functions, but won't give you want you want.
You might look for some jQuery plugins that can help you do this, most use the same principals behind Rich Text or other online/web-based HTML WYSIWYG editors. I've had trouble locating them myself.
These 3 situations (that I can think of right now) are pretty much what you will face natively with HTML4 or below, as text in an actual HTML4 INPUT textbox is pure text. It is not html and therefore NOT clickable. Here are some variations:
The INPUT tag's VALUE attribute, also referenced as the corresponding DOM object's "value" property (which is basically what you've been doing, and the most you can hope for, if you decide that you MUST have the text that's ACTUALLY inside the textbox (because the text inside the textbox is the VALUE attribute, as I have it with "http://yahoo.com" in this example):
<input id="myTxtbox" type="text" value="http://yahoo.com">
where the INPUT's VALUE = "http://yahoo.com", which you can retrieve with:
in pure javascript:
document.getElementById("myTxtbox").value
in jQuery:
$("myTxtBox").val()
When your link/url is the text in between the <INPUT> and </INPUT>, i.e. the text/innerText of the textbox. This is useless for your question/scenario since it's not clickable, and more importantly NOT INSIDE the textbox. However, someone might want to use this to retrieve any text that you may be using as a label (if you're not using the <label> tag itself already that is):
<input id="myTxtbox" type="text">
http://yahoo.com
</input>
The textbox's text/innerText is NOT an attribute here, only a DOM object property, but can still be retrieved:
pure javascript:
document.getElementById("myTxtbox").innerText
jQuery:
$("myTxtBox").text() -- you would use this to capure any text that you may be using as a label (if you're not using the tag).
The result being: http://yahoo.com
When your link/url is the form of an ANCHOR (<A>) with an HREF to your url (and visible link text) in between the <INPUT> and </INPUT>, i.e. the innerHTML of the textbox. This is getting a bit closer to what you want, as the link will appear as, and function as an actual link. However, it will NOT be inside of the textbox. It will be along side it as in example #2. Again, as stated in example #1, you CANNOT have actual working HTML, and therefore a working 'link' inside of a textbox:
<input id="myTxtbox" type="text">
<a href="http://yahoo.com">
http://yahoo.com
</a>
</input>
Once again, similarly to example #2, the textbox's innerHTML is NOT an attribute here, only a DOM object property, but can still be retrieved:
pure javascript:
document.getElementById("myTxtbox").innerHTML
jQuery:
$("myTxtBox").html()
The result being: http://yahoo.com

What is a no tag line interpreted as?

I'm currently wondering when to use clean text (not wrapped inside eg. <p> tags) in html documents.
i have a input fiels which i want some text before like:
<p>Age:</p> <input type="text" name="age">
But using the p tags as above will result in a linebreak between the two. However if I leave out the p tags this problem is no more.
My question is then wether it is OK to leave out the tags, and what in is interpreted as,
Thanks
You are looking for the <label> tag
Though there are many solutions as Webarto said you can style the p tag, or you can use span or label...People usually use label..I'll tell you why..
In good web designing principles one thing comes very important..
If you have some checkbox, or radiobutton, or textfield anything in your form then it should be selected just by clicking on the label assosiated with it..User should not search for the
radiobutton and then click, as it is very small, it should be triggered just by clicking the label, user should not search for the textfield and then click inside it and then type..
<label for="id of input element"> attribute provides that function
Hence people prefer
<label>
The p element means in principle a paragraph, though HTML5 (and common practice) takes a liberal position on this: a “paragraph” is any block of text. But even under that interpretation, there is no reason to use p markup for a field label, as you do not want the label to appear in a block of its own. You might use p markup around the label and the corresponding input field, as in
<p><label for=age>Age:</label> <input type=text name=age id=age></p>
The reason is that you probably want to present such constructs as blocks, not consecutively all on one line. But then you need to remember that p markup implies default margins, corresponding to an empty line above and below. You can remove then using CSS, but a simpler and somewhat more logical approach is perhaps to use div, which indicates a block but with no default margins;
<div><label for=age>Age:</label> <input type=text name=age id=age></div>

Can I embed HTML formatting inside of a <textarea> tag?

If I have a textarea block like so...
<textarea name="myTextArea" cols="100" rows="20" readonly=readonly>
.. text area content ...
</textarea>
How can I embed HTML formatted text inside this text block? If I can I had figured the following should work...
<textarea name="myTextArea" cols="100" rows="20" readonly=readonly>
<b>Hello how are you today?</b>
</textarea>
As an example. But that sentence does not show up as bold. Maybe I'm just trying to do something that can't be done?
I am pretty sure the answer is no -- cannot do it with a textarea.
From the MDN docs:
The HTML <textarea> element represents a multi-line plain-text editing control.
Permitted content Text
Is not possible with
<textarea>
Use this:
<div contenteditable="true">
</div>
You are correct, it cannot be done.
According to MDN, "character data" is the only permitted content for a <textarea>.
As other answers have described, what you are looking for is a WYSIWYG editor, like CKEditor, TinyMCE, or Kendo's Editor.
This is beside the point, but interestingly, MDN lists some examples on their HTMLTextAreaElement page on how to enable dynamic adding of HTML tags around text in a <textarea> and how to enable autogrow behavior for a <textarea>.
You can do this (convert the "special" characters):
<textarea name="myTextArea" cols="100" rows="20" readonly=readonly>
<b>Hello how are you today?</b>
</textarea>
Nope! If you validated that in an HTML document, you'd get an error along the lines of
document type does not allow element "b" here
What you might be looking for is a What-You-See-Is-What-You-Get editor, which is actually an <iframe> which is making use of a JavaScript feature designMode. It's possible to find tutorials on how to create these (archive), but it's a much, much better idea to use an existing one, as for it to be really useful and usable takes a lot of work.
Take note that - if you were still interested in validation, which you should be if you're working with HTML - is that you won't be able to use a strict DOCTYPE declaration, as the WYSIWYG editor will be using an iframe.
You can try this too its simple and if you are using asp.net use lable contron in text area add this as
Text="< stonename >"
"&lt";stonename"&gt";
You can not do that inside textarea but what you can do...you can use p tag and parse information to tag by JQuery. Something like this:
var stringviewinfo ="aaaaaaaaaaaaaaaaaa<br>aaaaaaaaaaaaaaaaaaaaaaaaa<br>aaaaaaaaaaaaaaaa<b>aaaaaaaa</b>aaaaaaaaaaaaaaaaaa<br>aaaaaaaaaaa<font color=\"red\">AAAA</font>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<b>aaaaaaaaaaaaa<b>aaaaaaaaaaaaaaaaaaaaaaaaaaaa<br>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</br>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
$('#viewinfo').html(stringviewinfo);
<p style="word-wrap: break-word;" id="viewinfo"></p>
I hope this will help.
By now (8 years later) you know textarea is not the way to go.
If you happen to be using Vue.js, you can use a div with v-html to set the inner HTML
<template>
<div name="myTextArea" cols="100" rows="20" readonly=readonly v-html="content" />
</template>
<script>
export default {
data() {
return{
content = "<b>Hello how are you today?</b>";
}
}
</script>
Here is the answer.
Use this function.
function br2nl(varTest) {
return varTest.replace(/<br\s*\/?>/ig, "\r");
}
There is a very simple way you can achieve that.
First: Customize the <textarea> to accept and save special chars to DB
<textarea contenteditable="true" class="form-control" name="setting[header_code]"><?php echo set_value('setting[header_code]', html_entity_decode($item->header_code)) ?></textarea>
Second: extract and echo the saved data from DB
<?php echo html_entity_decode(config(nl2br('header_code')));?>
Hope it can help you.
Thanks

How to customize form styled by django-uni-form?

I'm using django-uni-form to style my form using the filter my_form|as_uni_form:
<form class="uniForm" id="my_form" method="post" action="./">
<fieldset class="inlineLabels">
{{ my_form|as_uni_form }}
<div class="form_block">
<input type="submit" value="Submit"/>
</div>
</fieldset>
</form>
It looks really good. But I need to customize it.
For example, one of the field "percentage" of the form is of the type IntegerField. It is being rendered as an <input type="text">. The problem is that the text box is really wide, I'd like to make it only 2 character wide. Also I want to add a percentage sign "%" right after the text box so that users know they if they put in the number "10" in the text box, it means 10%.
Is there anyway to do that with django-uni-form?
Thanks for your help.
You'll need to loop over the elements of your form and render the uniForm markup yourself. Either that, you can customize the look of each input based on an id or class.
What I'd do is look at the mark up it generates, and then loop over the elements generating that same markup and customize them. See the Django docs for more information.
I have the same question as yours. I think the length of the text input is easy to change via css. I'm more concerned about the custom html element behind the input, in your case percentage mark. I don't find an easy solution to it. Looks like either we have to mimick the way a field is rendered in django-uni-form template or write a filter of our own. I'm still waiting for a more elegant solution.