modifying a view tree using jQuery - html

I have a text box and a button, i'm trying to figure some messaging functionality using a jQuery tutorial but having trouble finding a good direction. i want the input from the text box to appear in a DIV underneath the DIV containing "Sent messages appear here". testing using www.htmledit.squarefree.com to test the code out below and using F12 (dev mode) i can see the HTML tree
<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
<div>
<p>
<input type = "text"
id = "textfield" />
<button type ="button" id="btn">Add Message </button>
</p>
<Script>
$( "#btn" ).click(function() {
var message = $.trim($("#textfield").val())
$("#sent").append(message);
$("#sent").append("<br />");
});
</Script>
</div>
<div id="sent" style="font-style: italic">
<div>Sent messages appear here.</div>
</div>
The appended message if i type "U" in the textfield output fine on the bottom screen BUT the html tree does not have DIV tags. the following methods seem helpful but the references about them are a bit confusing. .html(), .text(), .append(), and $("html") functions
the tree should look like this in the element console.

I think that you append the text. The message doesn't wrap with 'div tag.
so to do this, It's very easy.
$("#sent").append('<div>' + message + '</div>');
$("#sent").append("<br />");
append function. It's append the element into the target.
you should add the html tag to append the message
html function. It's to get the children html element of the target.
text function. It's to get the all text for the target.
if you call text function, you only get the string without any html element.
It's a difference with html.
Html is get children html element.

Related

How can I navigate using anchor tag in SAPUI5?

I know how to do using html and JavaScript
<h2 id="C4">Chapter 4</h2>
Jump to Chapter 4
This is what I am trying in SAPUI5. On click to Back to top link it should navigate to helpButton. This is not working for me.
<Button id="helpButton" icon ="sap-icon://sys-help" />
<Link text="Back to top"
press="#helpButton"/>
You can actually do this in UI5. A little differently than how you tried though.
The problem is that the UI5 ID is not the same as the HTML ID (which is what you want to use with the hash-link for the browser to jump there). Also, you cannot specify a URL inside the press "attribute" of the link. The press "attribute" is in fact an event (so you can only specify an event handler name).
So to be able to do what you want, you have to use the href property of the Link and fill it with the HTML ID of the target control. You can do this on the onAfterRendering hook of the view (that's when you are able to find the HTML ID of the target control):
onAfterRendering: function() {
var oRef = this.byId("target").getDomRef();
this.byId("link").setHref("#" + oRef.id);
}
You can find a working fiddle here: https://jsfiddle.net/93mx0yvt/26/.

innerHTML doesn't add HTML elements permanently

I'm trying to understand the behavior of innerHTML in the code below. I want to permanently add a new div block every time I hit the button, but it seems that the new block only pops up for a split second then disappears.
Does anyone know why this is the case, and how to fix it?
Also, when I change the code to use appendChild instead of innerHTML, I get an error saying Argument 1 of Node.appendChild is not an object.. I'm not sure what this means.
Any help is much appreciated!
Below is the code:
<DOCTYPE html>
<html>
<body>
<form onSubmit="loadData()">
<input type="submit" id="button">
</form>
<div id="block">List of items:</div>
<script>
function loadData(){
document.getElementById("block").innerHTML += "<div>item</div>";
// document.getElementById("block").appendChild("<div>item</div>");
};
</script>
</body>
</html>
Because you are submitting then the page reloads and your HTML is obliterated.
If you need items to persist then you will need to use cookies, localStorage or a server-side solution.
function addItem()
{
document.getElementById("block").innerHTML += "<div>item</div>";
}
<DOCTYPE html>
<html>
<body>
<form>
<input type="button" id="button" onclick="addItem()" />
</form>
<div id="block">List of items:</div>
</body>
</html>
you are submitting the page. appendChild or innerHtml happen directly after submit, before the new page is loaded. once the new page is loaded, the current page (with the applied modifications) is dismissed and replaced with the new page.
if you wanted something to happen on the new page, you would need to execute the code on that page. (or don't use a form submit, but rather some ajax for sending the form).
The reason why appendChild is not working for you, is that appendChild expects a dom node as parameter, not a string. it would be like document.getElementById("foo).appendChild(document.createElement("div")). (the tricky part is that with createElement you get an empty element, you would also need to put the content you want into it.
Your first question is already answered by #lee.
Your problem with your second answer is, that you can not use appendChild like you did. If u want to use append child, according to the mozilla developer docs you will have to to something like this:
var mydiv = document.createElement("div");
mydiv.appendChild(document.createTextNode("item"));
document.getElementById("block").appendChild(mydiv);
to get the result you asked for.

How do you add the title attribute to dnn controls in dotnetnuke?

How do you add the title attribute to the DNN:Label control and have it render as a title attribute, <label title="myTitle">, in html?
Here is my code:
<div class="dnnFormItem">
<dnn:label id="lblDateNeeded" runat="server" controlname="DateNeeded" resourcekey="DateNeeded" />
<dnn:dnndatepicker runat="server" cssclass="dnnFormInput" id="DateNeeded" skin="Office2010Silver">
<Calendar UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False" ViewSelectorText="x" Skin="Office2010Silver"></Calendar>
<DateInput DisplayDateFormat="M/d/yyyy" DateFormat="M/d/yyyy" LabelWidth="40%"></DateInput>
<DatePopupButton ImageUrl="" HoverImageUrl=""></DatePopupButton>
</dnn:dnndatepicker>
</div>
this.Page.Title = "My Custom Title";
However, in DotNetNuke this will only work in the Page_PreRender method (verified in DotNetNuke 6.2.3).
If you want to set it earlier, you must still use this method which boils down to this:
((DotNetNuke.Framework.CDefault)this.Page).Title = "My Custom Title";
The above will work in Page_Load, Page_Init and Page_PreRender.
If you want to modularize it more, you can add the following in your base class for your modules (a good idea to always do this):
public DotNetNuke.Framework.CDefault BasePage {
get { return (DotNetNuke.Framework.CDefault)this.Page; }
}
And then simply use:
this.BasePage.Title = "My Custom Title";
The great thing about this method is that you can use it for the meta description and keywords as well.
this.BasePage.Description = "My Custom Description";
this.BasePage.Keywords = "My Custom Keywords";
Source
The title attribute is not accessible in the DNN label. As the OP noted, if specified as an attribute in the front end code, it will be ignored in the output HTML. Furthermore, if added in the code behind using YOURLABEL.Attributes.Add(), it will also be ignored.
Since it is not possible using the properties of the DNN label, another option is needed. One option is to address the issue with JQuery.
The HTML output by the DNN label looks like this:
<div class="dnnLabel">
<label>
<span id="#ASP_PATH#_#YOURID#">Test Label</span>
</label>
</div>
The following JQuery will set the title attribute of the label based on the text in the SPAN:
$('label').each(function () {
$(this).attr('title', $(this).text().trim());
})
Running this produced the following changes to the HTML:
<div class="dnnLabel">
<label title="Test Label">
<span id="#ASP_PATH#_#YOURID#">Test Label</span>
</label>
</div>
This function runs over all labels on the page. The JQuery selector could be modified to identify the labels you need to modify, if it's not everything on the page.
This may not produce the exact output you are looking for but you have plenty of flexibility with the JQuery function. If you need to set a special value for the label and you know what the text is going to be, you could use an if or switch to identify the specific label and process it accordingly.

HTML: How to make a readonly textarea copyable in iOS-devices

How do I make a textarea and input type="text" highlightable and copyable on iOS-devices?
This does not work:
<textarea readonly="readonly">Totally readonly, cannot be copied</textarea>
Neither does:
<textarea disabled="disabled">Totally readonly, cannot be copied</textarea>
EDIT: The text-area is constantly being updated, so a one-off transformation of it won't work.
The content of the textarea can also be HTML.
I have a JSFiddle that I tested this on: http://jsfiddle.net/sebnilsson/jfvWZ/
One solution could be to find all the readonly textareas on the page and render a div with the contents in place of the read only field. I have written some very simple JS to demonstrate this.
Something along the lines of
$('textarea[readonly]').removeAttr('readonly').each(function () {
var $this = $(this);
$this.hide().after('<div data-textarea="' + $this.attr('id')
+ '" class="textarea">' + $this.val() + '</div>');
}).on('textareachange', function () {
var $this = $(this);
$('[data-textarea="' + $this.attr('id') + '"]').html($this.val());
});
You will also need to trigger the event when you update the textarea value.
For example
$('textarea').val('test').trigger('textareachange');
There's a more extensive example here with examples on the styling etc.
http://jsfiddle.net/ssfUx/3/
I've successfull select some text on my iPhone, but needs many try.
<textarea readonly onfocus="this.blur();">Totally readonly, CAN BE copied</textarea>
and the last : http://jsfiddle.net/jfvWZ/6/
<div>
<label>Plain div</label><br />
<div id="plain-div" onFocus="this.blur();">
Plain div
</div>
</div>
Easy to select the text on iPhone
Likewise ran into this issue.
Not sure if the following is a decent, correct or semantic alternative, but it worked for me.
I simply changed the textarea to a div readonly, same styles applied.
The one drawback is that in JavaScript I couldn't target the div with this['myForm']. It doesn't appear to be a child of the form element in DOM.
Instead I had to get the element by id and set it's innerHTML, rather than set the value as with textarea.
It worked on Ipad 1 IOS5 and Iphone 4s IOS7 I am now able to select and copy text to clipboard.

avoid ie contentEditable element to create paragraphs on Enter key

On InternetExplorer, a contentEditable DIV creates a new paragraph (<p></p>) each time you press Enter whereas Firefox creates a <br/> tag.
Is it possible to force IE to insert a <br/> instead of a new paragraph ?
Here's a solution (uses jQuery). After you click on the 'Change to BR' button, the <br> tag will be inserted instead of the <p></p> tag.
Html:
<div id='editable' contentEditable="true">
This is a division that is content editable. You can position the cursor
within the text, move the cursor with the arrow keys, and use the keyboard
to enter or delete text at the cursor position.
</div>
<button type="button" onclick='InsertBR()'>Change to BR</button>
<button type="button" onclick='ViewSource()'>View Div source</button>
Javascript:
function InsertBR()
{
$("#editable").keypress(function(e) {
if (e.which == 13)
{
e.preventDefault();
document.selection.createRange().pasteHTML("<br/>")
}
});
}
function ViewSource()
{
var div = document.getElementById('editable');
alert('div.innerHTML = ' + div.innerHTML);
}
These links helped.
Working example here.
Yes it is possible to avoid the insertion of paragraphs by stopping the keydown event first (window.event.stopPropagation();) and then inserting the string by using insert HTML command.
However, IE depends on this divs for setting styles etc. and you will get into trouble using <br>s.
I suggest you using a project like TinyMCE or other editors and search for an editor which behaves the way you would like, since they have all kinds of workarounds for different browser issues. Perhaps you can find an editor which uses <br>s...
You can always learn to use SHIFT + ENTER for single line returns and ENTER for paragraph returns. IE behaves like MS Word in this respect.
Changing the line-height of the <p> inside the editable <div> works:
#editable_div p
{
line-height: 0px;
}
If you can use it, FCKEditor has a setting for this