Unexpected token illegal and five red dots show on JavaScript error? - javascript

I have gotten a javascript error when trying to remove an attribute of type required. 5 red dots have appeared behind the script which i am unsure what they mean.
How can i remove the attribute of required?
What are those red dots?
Edit:
$("#FileUpload").change(function () {
var fileName = $(this).val().replace("C:\fakepath\", ");
$("#FileName").html(fileName);
$("#HasNewFile").val("True");
$('#selectVehicleMake').removeAttr('required');​​​​​
$('#selectVehicleRange').removeAttr('required');​​​​​
$('#selectVehicleModelCode').removeAttr('required');​​​​​
$('#selectVehicleModel').removeAttr('required');​​​​​
$("#btnMatch").click();
});

I have had this in two occasions and found that in both it was because there were white spaces behind the code. Best fix i found is to cut that code and paste it into note pad, the make sure there are no white spaces behind the text. You can check this by clicking behind the text and clicking backspace.

Copying code from some code - "fields" > like stackoverflows have a hidden char in the element.
After copying to VS:
When copied into Text Editor (i used brackets)
I've found that the dot appears while inspecting the copied element:
So it seems to stem from jquery's prettyprint. and this is what it is

Related

Count displayed lines in textarea using JavaScript or jQuery

When I say line, I mean wrapped line as displayed in the textarea. A long line will wrap to multiple lines. If a line (ending with newline) wraps into three lines, the count has to be three.
The problem is that line breaks happen not just at spaces but also at other character sequences. For example in Firefox, aaaaaa*<bbbbbb will break after * if there's not enough space in the same line.
What this means is that we can't simply use the number of characters that can fit in the textarea's width and spaces to figure out the number of displayed lines. Obviously, counting newlines is not useful since it ignores wrapping. There's a way to force newline insertion at line breaks by using the textarea attribute wrap="hard". As I understand, this happens only when the form is submitted to server. This is not a suitable solution since we wish to do counting in JavaScript.
Another variant of the problem question is, "Is there a way to move the cursor to next displayed line?" I tried the following but this doesn't move the cursor at all, it only "mimics" down arrow key event:
var e = $.Event( "keydown", { which: 40 } );
...
$(this).trigger(e);

Unable to append space to textarea content

This one is a strange one and I simply cannot get my head around it. We are trying to append space to a textarea content using the following jquery,
var checkThis ='help';
$('#msg').val(checkThis+' ');
$('#msg').focus();
But when we focus on the text area we can see that the space is not added. Also, the cursor doesn't focus automatically.
I am not able to figure out if there is any other script in the code doing this. This code is actually a large piece of maintenance code, is there anyway I can find what function may be trimming the text?
Try this:
$('#msg').append(checkThis+' ').focus();

Contenteditable div: save and restore caret position when is positioned in empty new line

I need to save and restore the caret position as the user types in a contenteditable div (the html written is edited and re-inserted with each key pressed).
I've read and succesfully used this solution by Tim Down to a similar question: https://stackoverflow.com/a/13950376/2086428.
The problem occurs when the caret is positioned in an empty line, it will be restored to the previous non-empty line (try it here, add a new line and save / restore the cursor).
In the comments section of the solution proposed one user had the same problem, the author of the solution hinted to convert the <br>s into characters.
How can I do this?
Are there any simpler solutions?
PS: I can't use rangy for this project.
From this answer:
The solution is to make sure, that every "br" tag is followed by a newline character (\n).
This won't hurt the HTML content in any visible way, but now all HTML breaks are translated to plain text line breaks as soon as range.toString() is being called.
<br>\n
Working example here: http://jsfiddle.net/gEhjZ/95/
For comparison, example, which has problem (without \n after "br"): http://jsfiddle.net/gEhjZ/94/
You can listen to keydown; every time Enter key is pressed check if there are some character(s) in the same (or next sibling) text node immediately after caret position (you will receive event before the line is broken). If not add after caret position and then save the position.
Some of these utilities can be useful:
https://github.com/MailOnline/milo/blob/master/lib/util/dom.js
https://github.com/MailOnline/milo/blob/master/lib/util/selection/index.js

Preserving Newlines When Using ".text" or ".textContent". Possible? Alternatives? Workarounds?

If I grab some html from one element, then attempt to assign it as the text content of another element, newlines are not preserved (at least not in the latest Firefox and Chromium).
So, for example, the follow code (with sensible html) produces output where the newlines are replaced by spaces. Well, except the alert, which works as expected.
$("#info").data("html", $("#info").html());
$("#jquery").text($("#info").data("html"));
document.getElementById("javascript").textContent = $("#info").data("html");
$("#alert").click(function() { alert($("#info").data("html")) });
Here's a running example: http://jsfiddle.net/76S7z/2/
There should be some method of setting the html of one element as the text of another while preserving newlines properly.
Is this possible with "text" or "textContent"? Is there an alternative way to do this? Is there a simple workaround? A less than simple workaround?
As you've already determined, Web browsers don't normally render newline characters \n as line breaks. If you're resistent to adding the line break element <br />, you can use the white-space CSS property with the value pre-line, which will:
Sequences of whitespace are collapsed. Lines are broken at newline characters, at <br>, and as necessary to fill line boxes.
Be sure to check the property's compatibility tables before using.
<div style="white-space: pre-line;">
Look
at
these line breaks!
</div>
Here's a JSFiddle example.

Remove extra br tags with JS

I have a post which contains extra line breaks and want to limit the post to only show one linebreak. I thought this css might work..
br+br{display:none}
but since the text is not wrapped in its own element all the line breaks in the post are siblings and this doesn't work... now I am trying to solve this with JS...
content.replace(/<br><br>/g,'<br>')
Why is this only replacing the first set of linebreaks that are next to each other? (I need to run it multiple times to get the effect I want of all uneccesary line breaks being removed)
and what should I do instead?
If your regex represents your HTML exactly, this should work:
content.replace(/(<br>)+/g,'<br>')
Although your CSS should've worked: http://jsfiddle.net/UvVbE/

Categories