Line break inside quotes in an HTML attribute? [closed] - html

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have this HTML:
<form action="mypage.php" method="post"
onsubmit="return confirm('Are you sure you want to submit this form? Submitting this form has serious consequences and you should definitely think long and hard before doing it.');">
I want to keep my code at or below 80 characters wide. How can I split this up into multiple lines while retaining the exact same functionality?

I'ld create a Javascript section that defines a function that you call from the form:
<script type="text/javascript">
function formSubmit() {
return confirm('Are you sure you want to submit this form? Submitting' +
' this form has serious consequences and you should ' +
'definitely think long and hard before doing it.');
}
</script>
the form:
<form action="mypage.php" method="post" onsubmit="formSubmit()">
This gets your Javascript out of you HTML form and can be clearly broken where you like.

Do as you want. It is not important to the HTML as long as you avoid doing this for blocks like <PRE>. There's lot of tools like HTML Tidy, HTML beautify, HTML format you could google for and try. Or hit ENTER by hand from time to time
EDIT
I seem to not really get the question at first. If you need to split the JS arguments, slice the strings into smaller blocks and then concatenate it back using +, so instead of
alert('long line here');
you cold write:
alert('long ' +
'line ' +
'here');
but it would be better to separate JS from HTML so your onSubmit should calling the function, instead of holding the end JS code.

I would definitely recommend removing the inline Javascript and keeping things separated. You could even go so far as to remove the onsubmit function and just capture the event in your Javascript itself.
You could break up the string and concatonate it again as has been proposed to get a quick solution, but if you would like to style the confirmation box even more, I would suggest looking into something like http://onehackoranother.com/projects/jquery/boxy/ or http://jqueryui.com/dialog/#modal-confirmation for additional solution options.

Related

Suggestions for differentiating which Q a user would like to answer

I am trying to make it so that a user can choose which question they want to answer, and, using a textarea, answer that question. I'm having trouble figuring out how I can differentiate which question the user wants to answer. I've tried copying the html code for the textarea form into a variable, and then printing it foreach keys %hash, but I just wind up with syntax errors all over the place. My perl code takes each question in a separate .txt file and stores them into a hash in the form of question/answer pairs. If a question doesn't have an answer, the value is undef., I believe. Does anyone have any suggestions? I'm not asking for a direct answer, but rather some guidance from more experienced or insightful programmers. Any advice is appreciated. Thanks! Here is the most important code:
perl:
my #qa_list = ($pagev->listFile($anslist), $pagev->listFile($unanslist));
my %questions = map { split(/\t/, $_, 2) } #qa_list;
my $answer = $cgi->param('answer');
my $questionslist = join("<br>", #qa_list);
my $aselect = $cgi->param('select');
html:
<form action="/home/megaoff/www/viewquestions.dhtml" method="post">
Add an Answer: <br>
<textarea rows="1" cols="50" name="answer">
Add an answer here
</textarea>
<input type="submit" value="submit"/>
</form>
</body>
</html>
It's a little scattered right now, mostly because I'm still experimenting with things. But I hope the gist is there: Suggestions for differentiating which question the user wants to answer.

Change color randomly [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I have a paragraph or div.
HTML
<p style="color:red;">Change color randomly</p>
this code will show the
but I want this type of output using CSS any idea
Thanks
You can't do this with pure CSS where it's really random, you'll need some JavaScript for that. I created a little demo that also uses jQuery & it generates different colours each time you reload the page.
HTML
<p>Change color randomly</p>
JavaScript
// Define some colours
var colours = [
"red",
"orange",
"yellow",
"green",
"blue",
"purple"
];
// Retrieve the words
var text = $("p").html().split(" ");
// Empty the elment
$("p").empty();
// Iterate over the words
$.each(text, function(i, word) {
if(i != text.length) {
word += " "; // Add space after word
}
// Get random color
var colourIndex = Math.floor(Math.random() * colours.length);
$("<span>")
.html(word)
.css("color", colours[colourIndex])
.appendTo($("p"));
});
JSFiddle.
There is direct pseudo class to style the first-letter but not the first word, so you need to add any inline elements to get it styled.
Use nth-child with span tag
<p style="color:red;"><span>Change</span> color <span>randomly</span></p>
span:nth-child(1){
color:green
}
span:nth-child(2){
color:violet
}
DEMO
Pure CSS is not possible. You will have to use Javascript to code this feature. Here is a link that probably helps you to get started: http://paulirish.com/2009/random-hex-color-code-snippets/
Below css will do it.
<p style="color:red;"><span style="color:#909">Change</span> color <span style="color:blue">randomly</span></p>

How to insert a space between two variables in JSP? [duplicate]

This question already has answers here:
How do I make JSP tag files NOT ignore all whitespace?
(4 answers)
Closed 8 years ago.
I have one line of html in a form on a JSP page like this:
<c:forEach var="entry" items="${set}">
<input type="checkbox" name="thing" value="${entry.key} ${entry.value}">
</c:forEach>
However, the space between the key and the value ${entry.key} ${entry.value} is lost when the form is submitted. I've tried using a \ before the , in that case both the \ and the are still there when submitting.
It seems that Java EL does not preserve isolated spaces, is that right? If so, is there a valid workaround?
EDIT: This is so silly of me, many thanks to Cthulhu. But I still don't understand the behavior after adding a \. Why would that cause the space to show?
You can insert white-spaces using:
Character Entity References
Edit: Seems to be a bug in the way spaces are handled by the EL parser, so you should use either html entities like or the other ways outlined here.
${entry.key}${' '}${entry.value}
was my way to fix this. Seems a little less verbose than the
<c:out value="${bean.foo} ${bean.bar} ${bean.waa}" />
from the other thread.

Regular expression to remove HTML tags from a string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular expression to remove HTML tags
Is there an expression which will get the value between two HTML tags?
Given this:
<td class="played">0</td>
I am looking for an expression which will return 0, stripping the <td> tags.
You should not attempt to parse HTML with regex. HTML is not a regular language, so any regex you come up with will likely fail on some esoteric edge case. Please refer to the seminal answer to this question for specifics. While mostly formatted as a joke, it makes a very good point.
The following examples are Java, but the regex will be similar -- if not identical -- for other languages.
String target = someString.replaceAll("<[^>]*>", "");
Assuming your non-html does not contain any < or > and that your input string is correctly structured.
If you know they're a specific tag -- for example you know the text contains only <td> tags, you could do something like this:
String target = someString.replaceAll("(?i)<td[^>]*>", "");
Edit:
Ωmega brought up a good point in a comment on another post that this would result in multiple results all being squished together if there were multiple tags.
For example, if the input string were <td>Something</td><td>Another Thing</td>, then the above would result in SomethingAnother Thing.
In a situation where multiple tags are expected, we could do something like:
String target = someString.replaceAll("(?i)<td[^>]*>", " ").replaceAll("\\s+", " ").trim();
This replaces the HTML with a single space, then collapses whitespace, and then trims any on the ends.
A trivial approach would be to replace
<[^>]*>
with nothing. But depending on how ill-structured your input is that may well fail.
You could do it with jsoup http://jsoup.org/
Whitelist whitelist = Whitelist.none();
String cleanStr = Jsoup.clean(yourText, whitelist);

What is the preferred way to indent cases in a switch? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
As I was writing another switch in Eclipse, I once again came across a rather weird (to me, at least) default indentation, which is applied to 'switch' statements:
switch (i) {
case 1:
...
case n:
...
}
I tend to prefer another way:
switch (i) {
case 1:
...
case n:
...
}
Which way is more readable and eye-pleasing for you? I'm still not hundred percent determined what's best for me, so I'd like to stick to what's best for other people who would read my code.
BTW, you're free to close this question if this is too subjective. :)
According to the "official" Java Code Conventions, it's the first variant (no additional indentation for case).
I prefer the second way as well. But it is more important to stay consistent within a particular application and/or within a particular team than it is to indent one way or the other.
I tend to indent all control structure bodies a single (4space) tab like so:
switch (i)
{
case 1:
...
case n:
...
}
I consider the switch to be the outer control structure and the case directives part of the body(even though they are part of the control structure).
I would then further tab indent each case like so:
switch (i)
{
case 1:
do_something();
case n:
do_something_else();
}
I find this to be the most readable format for the switch case construct.
As jkohlhepp has mentioned conformity to the code style conventions of the project you are working on is the most important thing, if you are working on a project that has none, it is worth developing some.
I use second way:
switch (i) {
case 1:
...
case n:
...
}
The first is the standard switch case indentation.
switch (i) {
case 1:
.... // Here you can write more code in a row than using the second way
break;
case n:
....
break;
default:
....
break;
}
Notice the new inserted line between switch (i) and case 1:
The first method makes logical sense (to me), however I also prefer the second method. I think most of us are conditioned to expect that code inside braces will be indented.