Rectangle 27 77

```HTML
```html
<h2>Example of code</h2>

<pre>
    <div class="container">
        <div class="block two first">
            <h2>Your title</h2>
            <div class="wrap">
            //Your content
            </div>
        </div>
    </div>
</pre>
```

@tonymx227 It's a feature of the redcarpet library which GitHub uses for Markdown rendering: github.com/vmg/redcarpet. See the "fenced code blocks" option.

@IvanZuzak Thank you - it works for me. It seems you have to make sure you put your HTML is in the second line as you have shown in your example.

markdown - HTML code into README.md on Github - Stack Overflow

github markdown
Rectangle 27 76

```HTML
```html
<h2>Example of code</h2>

<pre>
    <div class="container">
        <div class="block two first">
            <h2>Your title</h2>
            <div class="wrap">
            //Your content
            </div>
        </div>
    </div>
</pre>
```

@tonymx227 It's a feature of the redcarpet library which GitHub uses for Markdown rendering: github.com/vmg/redcarpet. See the "fenced code blocks" option.

@IvanZuzak Thank you - it works for me. It seems you have to make sure you put your HTML is in the second line as you have shown in your example.

markdown - HTML code into README.md on Github - Stack Overflow

github markdown
Rectangle 27 454

Using $('#myDiv').click(function(){ is better as it follows standard event registration model. (jQuery internally uses addEventListener and attachEvent).

Using $('#myDiv').click(function(){ is better as it follows standard event registration model. (jQuery internally uses addEventListener and attachEvent).

Basically registering an event in modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call addEventListener() for the same target.

Basically registering an event in modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call addEventListener() for the same target.

var myEl = document.getElementById('myelement');

myEl.addEventListener('click', function() {
    alert('Hello world');
}, false);

myEl.addEventListener('click', function() {
    alert('Hello world again!!!');
}, false);
var myEl = document.getElementById('myelement');

myEl.addEventListener('click', function() {
    alert('Hello world');
}, false);

myEl.addEventListener('click', function() {
    alert('Hello world again!!!');
}, false);

addEventListener is the way to register an event listener as specified in W3C DOM. Its benefits are as follows:

addEventListener is the way to register an event listener as specified in W3C DOM. Its benefits are as follows:

  • It allows adding more than a single handler for an event. This is particularly useful for DHTML libraries or Mozilla extensions that need to work well even if other libraries/extensions are used.
  • It allows adding more than a single handler for an event. This is particularly useful for DHTML libraries or Mozilla extensions that need to work well even if other libraries/extensions are used.
  • It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling)
  • It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling)
  • It works on any DOM element, not just HTML elements.
  • It works on any DOM element, not just HTML elements.
<button onclick="alert('Hello world!')">

Other methods such as setting the HTML attributes, example:

myEl.onclick = function(event){alert('Hello world');};
<button onclick="alert('Hello world!')">

HTML attribute should be avoided as It makes the markup bigger and less readable. Concerns of content/structure and behavior are not well-separated, making a bug harder to find.

myEl.onclick = function(event){alert('Hello world');};

are old and they can be over written easily.

The problem with the DOM element properties method is that only one event handler can be bound to an element per event.

Lets say you run the $('#myDiv').click(function(){ code first, then you generate 20 rows of HTML dynamically from JavaScript and each row has a button on it that when clicked the JavaScript is required to execute that same function. If you do that first then it won't work as the event handler has been added before the HTML has been generated. It would seem easier to just throw in the onclick="functionName()" into the dynamically generated HTML then the button works straight away. Or do you know of a more elegant solution for this situation?

HTML attribute should be avoided as It makes the markup bigger and less readable. Concerns of content/structure and behavior are not well-separated, making a bug harder to find.

The problem with the DOM element properties method is that only one event handler can be bound to an element per event.

@zuallauz for that case jQuery offers .delegate() function. It will attach event to any element that will appear in the future on the site.

Lets say you run the $('#myDiv').click(function(){ code first, then you generate 20 rows of HTML dynamically from JavaScript and each row has a button on it that when clicked the JavaScript is required to execute that same function. If you do that first then it won't work as the event handler has been added before the HTML has been generated. It would seem easier to just throw in the onclick="functionName()" into the dynamically generated HTML then the button works straight away. Or do you know of a more elegant solution for this situation?

@zuallauz the jQuery .live() function might be an simpler solution for what you are after http://api.jquery.com/live/. A good article on the differences between .bind(), .live() and .delegate() here

@SimonRobb .live is deprecated. Use .delegate for older versions or use .on for newer jQuery versions.

@zuallauz for that case jQuery offers .delegate() function. It will attach event to any element that will appear in the future on the site.

@SimonRobb .live is deprecated. Use .delegate for older versions or use .on for newer jQuery versions.

@Jon Your disagreement is based on bad coding practices. If the code is ugly, you should consider refactoring it.. but not disagree on a better approach just because it is hard to find reference in a ugly code. Also, It is easy to find the reference if you follow a simple rule to bind the handler only by an ID or class. Any other way such as binding using traversal methods is bad coding and you cannot blame the standard if it is poor code.

@Vega, Good points. What about readability? Now you have to search all referenced JS files on the page to see all click handlers of an element by element's ID instead of searching for the function name. What's your take on this?

I agree with @supertonsky. I STRONGLY disagree that $('#myDiv').click(function(){ is better. In a large javascript application binding with an event becomes massively difficult to find all the references binding to that target. Is it the binding on a class, an id, a child references to a html tag? And what happens if css changes and class names you bound to need to be changed? In my experience working with others code it becomes very ugly very fast.

javascript - jQuery.click() vs onClick - Stack Overflow

javascript html javascript-events jquery
Rectangle 27 447

Using $('#myDiv').click(function(){ is better as it follows standard event registration model. (jQuery internally uses addEventListener and attachEvent).

Basically registering an event in modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call addEventListener() for the same target.

var myEl = document.getElementById('myelement');

myEl.addEventListener('click', function() {
    alert('Hello world');
}, false);

myEl.addEventListener('click', function() {
    alert('Hello world again!!!');
}, false);

addEventListener is the way to register an event listener as specified in W3C DOM. Its benefits are as follows:

  • It allows adding more than a single handler for an event. This is particularly useful for DHTML libraries or Mozilla extensions that need to work well even if other libraries/extensions are used.
  • It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling)
  • It works on any DOM element, not just HTML elements.

Other methods such as setting the HTML attributes, example:

<button onclick="alert('Hello world!')">
myEl.onclick = function(event){alert('Hello world');};

are old and they can be over written easily.

HTML attribute should be avoided as It makes the markup bigger and less readable. Concerns of content/structure and behavior are not well-separated, making a bug harder to find.

The problem with the DOM element properties method is that only one event handler can be bound to an element per event.

Lets say you run the $('#myDiv').click(function(){ code first, then you generate 20 rows of HTML dynamically from JavaScript and each row has a button on it that when clicked the JavaScript is required to execute that same function. If you do that first then it won't work as the event handler has been added before the HTML has been generated. It would seem easier to just throw in the onclick="functionName()" into the dynamically generated HTML then the button works straight away. Or do you know of a more elegant solution for this situation?

@zuallauz for that case jQuery offers .delegate() function. It will attach event to any element that will appear in the future on the site.

@SimonRobb .live is deprecated. Use .delegate for older versions or use .on for newer jQuery versions.

@Vega, Good points. What about readability? Now you have to search all referenced JS files on the page to see all click handlers of an element by element's ID instead of searching for the function name. What's your take on this?

I agree with @supertonsky. I STRONGLY disagree that $('#myDiv').click(function(){ is better. In a large javascript application binding with an event becomes massively difficult to find all the references binding to that target. Is it the binding on a class, an id, a child references to a html tag? And what happens if css changes and class names you bound to need to be changed? In my experience working with others code it becomes very ugly very fast.

javascript - jQuery.click() vs onClick - Stack Overflow

javascript html javascript-events jquery
Rectangle 27 447

Using $('#myDiv').click(function(){ is better as it follows standard event registration model. (jQuery internally uses addEventListener and attachEvent).

Basically registering an event in modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call addEventListener() for the same target.

var myEl = document.getElementById('myelement');

myEl.addEventListener('click', function() {
    alert('Hello world');
}, false);

myEl.addEventListener('click', function() {
    alert('Hello world again!!!');
}, false);

addEventListener is the way to register an event listener as specified in W3C DOM. Its benefits are as follows:

  • It allows adding more than a single handler for an event. This is particularly useful for DHTML libraries or Mozilla extensions that need to work well even if other libraries/extensions are used.
  • It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling)
  • It works on any DOM element, not just HTML elements.

Other methods such as setting the HTML attributes, example:

<button onclick="alert('Hello world!')">
myEl.onclick = function(event){alert('Hello world');};

are old and they can be over written easily.

HTML attribute should be avoided as It makes the markup bigger and less readable. Concerns of content/structure and behavior are not well-separated, making a bug harder to find.

The problem with the DOM element properties method is that only one event handler can be bound to an element per event.

Lets say you run the $('#myDiv').click(function(){ code first, then you generate 20 rows of HTML dynamically from JavaScript and each row has a button on it that when clicked the JavaScript is required to execute that same function. If you do that first then it won't work as the event handler has been added before the HTML has been generated. It would seem easier to just throw in the onclick="functionName()" into the dynamically generated HTML then the button works straight away. Or do you know of a more elegant solution for this situation?

@zuallauz for that case jQuery offers .delegate() function. It will attach event to any element that will appear in the future on the site.

@SimonRobb .live is deprecated. Use .delegate for older versions or use .on for newer jQuery versions.

@Vega, Good points. What about readability? Now you have to search all referenced JS files on the page to see all click handlers of an element by element's ID instead of searching for the function name. What's your take on this?

I agree with @supertonsky. I STRONGLY disagree that $('#myDiv').click(function(){ is better. In a large javascript application binding with an event becomes massively difficult to find all the references binding to that target. Is it the binding on a class, an id, a child references to a html tag? And what happens if css changes and class names you bound to need to be changed? In my experience working with others code it becomes very ugly very fast.

javascript - jQuery.click() vs onClick - Stack Overflow

javascript html javascript-events jquery
Rectangle 27 447

Using $('#myDiv').click(function(){ is better as it follows standard event registration model. (jQuery internally uses addEventListener and attachEvent).

Basically registering an event in modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call addEventListener() for the same target.

var myEl = document.getElementById('myelement');

myEl.addEventListener('click', function() {
    alert('Hello world');
}, false);

myEl.addEventListener('click', function() {
    alert('Hello world again!!!');
}, false);

addEventListener is the way to register an event listener as specified in W3C DOM. Its benefits are as follows:

  • It allows adding more than a single handler for an event. This is particularly useful for DHTML libraries or Mozilla extensions that need to work well even if other libraries/extensions are used.
  • It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling)
  • It works on any DOM element, not just HTML elements.

Other methods such as setting the HTML attributes, example:

<button onclick="alert('Hello world!')">
myEl.onclick = function(event){alert('Hello world');};

are old and they can be over written easily.

HTML attribute should be avoided as It makes the markup bigger and less readable. Concerns of content/structure and behavior are not well-separated, making a bug harder to find.

The problem with the DOM element properties method is that only one event handler can be bound to an element per event.

Lets say you run the $('#myDiv').click(function(){ code first, then you generate 20 rows of HTML dynamically from JavaScript and each row has a button on it that when clicked the JavaScript is required to execute that same function. If you do that first then it won't work as the event handler has been added before the HTML has been generated. It would seem easier to just throw in the onclick="functionName()" into the dynamically generated HTML then the button works straight away. Or do you know of a more elegant solution for this situation?

@zuallauz for that case jQuery offers .delegate() function. It will attach event to any element that will appear in the future on the site.

@SimonRobb .live is deprecated. Use .delegate for older versions or use .on for newer jQuery versions.

@Vega, Good points. What about readability? Now you have to search all referenced JS files on the page to see all click handlers of an element by element's ID instead of searching for the function name. What's your take on this?

I agree with @supertonsky. I STRONGLY disagree that $('#myDiv').click(function(){ is better. In a large javascript application binding with an event becomes massively difficult to find all the references binding to that target. Is it the binding on a class, an id, a child references to a html tag? And what happens if css changes and class names you bound to need to be changed? In my experience working with others code it becomes very ugly very fast.

javascript - jQuery.click() vs onClick - Stack Overflow

javascript html javascript-events jquery
Rectangle 27 447

Using $('#myDiv').click(function(){ is better as it follows standard event registration model. (jQuery internally uses addEventListener and attachEvent).

Basically registering an event in modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call addEventListener() for the same target.

var myEl = document.getElementById('myelement');

myEl.addEventListener('click', function() {
    alert('Hello world');
}, false);

myEl.addEventListener('click', function() {
    alert('Hello world again!!!');
}, false);

addEventListener is the way to register an event listener as specified in W3C DOM. Its benefits are as follows:

  • It allows adding more than a single handler for an event. This is particularly useful for DHTML libraries or Mozilla extensions that need to work well even if other libraries/extensions are used.
  • It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling)
  • It works on any DOM element, not just HTML elements.

Other methods such as setting the HTML attributes, example:

<button onclick="alert('Hello world!')">
myEl.onclick = function(event){alert('Hello world');};

are old and they can be over written easily.

HTML attribute should be avoided as It makes the markup bigger and less readable. Concerns of content/structure and behavior are not well-separated, making a bug harder to find.

The problem with the DOM element properties method is that only one event handler can be bound to an element per event.

Lets say you run the $('#myDiv').click(function(){ code first, then you generate 20 rows of HTML dynamically from JavaScript and each row has a button on it that when clicked the JavaScript is required to execute that same function. If you do that first then it won't work as the event handler has been added before the HTML has been generated. It would seem easier to just throw in the onclick="functionName()" into the dynamically generated HTML then the button works straight away. Or do you know of a more elegant solution for this situation?

@zuallauz for that case jQuery offers .delegate() function. It will attach event to any element that will appear in the future on the site.

@SimonRobb .live is deprecated. Use .delegate for older versions or use .on for newer jQuery versions.

@Vega, Good points. What about readability? Now you have to search all referenced JS files on the page to see all click handlers of an element by element's ID instead of searching for the function name. What's your take on this?

I agree with @supertonsky. I STRONGLY disagree that $('#myDiv').click(function(){ is better. In a large javascript application binding with an event becomes massively difficult to find all the references binding to that target. Is it the binding on a class, an id, a child references to a html tag? And what happens if css changes and class names you bound to need to be changed? In my experience working with others code it becomes very ugly very fast.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

javascript - jQuery.click() vs onClick - Stack Overflow

javascript html javascript-events jquery
Rectangle 27 91

.text()
var text = $("<p> example ive got a string</P>").text();

Update: As Brilliand points out below, if the input string does not contain any tags and you are unlucky enough, it might be treated as a CSS selector. So this version is more robust:

var text = $("<div/>").html("<p> example ive got a string</P>").text();

It's dangerous to put the HTML directly into $(), since that could interpret it in other ways (i.e. as a CSS selector). Use $("<div/>").html("<p> example ive got a string</P>").text() instead.

@Brilliand: Good point. I incorporated the suggestion into the answer, thanks!

Is this completely reliable? While it strips tags, it seems to execute any javascript inserted into an onLoad attribute of that tag if the tag supports that attribute. For example, if you use the string <img src="some_real_image.jpg" onLoad="alert('hi');"> , the javascript executes when running this to strip its tags.

@Nile: jQuery does this by creating DOM nodes and getting their pure-text values (effectively letting the browser do the hard work), so scripts will be executed if appropriate. There's no way to prevent this AFAIK.

When working with a dynamic content, in case the content is a plain text, not an html, this will cause error. So I use $('<span>'+content+'</span>').text();

How to strip HTML tags with jQuery? - Stack Overflow

jquery
Rectangle 27 57

function escapeHTML( string )
{
    var pre = document.createElement('pre');
    var text = document.createTextNode( string );
    pre.appendChild(text);
    return pre.innerHTML;
}

The function doesn't escape single and double quotes, which if used in the wrong context, may still lead to XSS. For example:

var userWebsite = '" onmouseover="alert(\'gotcha\')" "';
 var profileLink = '<a href="' + escapeHtml(userWebsite) + '">Bob</a>';
 var div = document.getElemenetById('target');
 div.innerHtml = profileLink;
 // <a href="" onmouseover="alert('gotcha')" "">Bob</a>

It is a great solution, although it does have a dependency on the DOM. If you are using JavaScript outside of a browser, you will need one of the other solutions below.

It does not escape quotes and you might incorrectly assume that it's safe to insert the content as HTML. Example: benv.ca/2012/10/2/you-are-probably-misusing-DOM-text-methods

limc, PLEASE UPDATE THIS WITH A SECURE SOLUTION. I downvoted it for now as its scary people out there may be implementing this -- I WILL TAKE OFF DOWNVOTE AND THEN UPVOTE WHEN I SEE YOU'VE UPDATED YOUR ANSWER. Thx!

How do I escape some html in javascript? - Stack Overflow

javascript html-encode
Rectangle 27 1

You could use an HTML <form>. Example:

<% using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "myForm" })) { %>
    <input type="text" name="foo" />
    <input type="text" name="bar" />
    <input type="text" name="baz" />
    <input type="submit" value="go go" />
<% } %>
$(function() {
   $('#myForm').submit(function() {
       $.ajax({
           url: this.action,
           type: this.method,
           data: $(this).serialize(),
           success: function(result) {
               alert(result.message);
           }
       });
       return false;
   });
});

and then have a controller action which will handle the submission:

[HttpPost]
public ActionResult Index(string foo, string bar, string baz)
{
    // TODO: process something ...
    return Json(new { message = "Thanks for submitting" });
}

Also if you want to use a normal link instead of a <form> with submit button don't forget to review your previous question.

jquery - How to pass values from a ascx page to controller - Stack Ove...

jquery asp.net asp.net-mvc
Rectangle 27 78

You can do this with a data URL. This includes the entire document in a single string of HTML. For example, the following HTML:

<html><body>foo</body></html>

can be encoded as this:

data:text/html;charset=utf-8,%3Chtml%3E%3Cbody%3Efoo%3C/body%3E%3C/html%3E

and then set as the src attribute of the iframe. Example.

Edit: The other alternative is to do this with Javascript. This is almost certainly the technique I'd choose. You can't guarantee how long a data URL the browser will accept. The Javascript technique would look something like this:

var iframe = document.getElementById('foo'),
    iframedoc = iframe.contentDocument || iframe.contentWindow.document;

iframedoc.body.innerHTML = 'Hello world';
Blocked a frame with origin "http://localhost" from accessing a cross-origin frame

@AndrewSwan I don't quite understand the problem with the single quotes. Can you give me an example?

For anyone like me who was looking how to encode HTML this way with php, you want rawurlencode (php.net/manual/en/function.rawurlencode.php)

Note that if you use innerHTML browser will not execute descendant script tags. For more information check Security considerations section of Element.innerHTML MDN page.

Html code as IFRAME source rather than a URL - Stack Overflow

html iframe frame
Rectangle 27 78

You can do this with a data URL. This includes the entire document in a single string of HTML. For example, the following HTML:

<html><body>foo</body></html>

can be encoded as this:

data:text/html;charset=utf-8,%3Chtml%3E%3Cbody%3Efoo%3C/body%3E%3C/html%3E

and then set as the src attribute of the iframe. Example.

Edit: The other alternative is to do this with Javascript. This is almost certainly the technique I'd choose. You can't guarantee how long a data URL the browser will accept. The Javascript technique would look something like this:

var iframe = document.getElementById('foo'),
    iframedoc = iframe.contentDocument || iframe.contentWindow.document;

iframedoc.body.innerHTML = 'Hello world';
Blocked a frame with origin "http://localhost" from accessing a cross-origin frame

@AndrewSwan I don't quite understand the problem with the single quotes. Can you give me an example?

For anyone like me who was looking how to encode HTML this way with php, you want rawurlencode (php.net/manual/en/function.rawurlencode.php)

Note that if you use innerHTML browser will not execute descendant script tags. For more information check Security considerations section of Element.innerHTML MDN page.

Html code as IFRAME source rather than a URL - Stack Overflow

html iframe frame
Rectangle 27 38

You can add style for :after a like html code.For example:

var value = 22;
body.append('<style>.wrapper:after{border-top-width: ' + value + 'px;}</style>');
$( "<style>.wrapper:after { border-top-width: " + value + "px; }</style>" ).appendTo( "head" )

Access the css ":after" selector with jQuery - Stack Overflow

jquery css jquery-selectors
Rectangle 27 38

You can add style for :after a like html code.For example:

var value = 22;
body.append('<style>.wrapper:after{border-top-width: ' + value + 'px;}</style>');
$( "<style>.wrapper:after { border-top-width: " + value + "px; }</style>" ).appendTo( "head" )

Access the css ":after" selector with jQuery - Stack Overflow

jquery css jquery-selectors
Rectangle 27 38

You can add style for :after a like html code.For example:

var value = 22;
body.append('<style>.wrapper:after{border-top-width: ' + value + 'px;}</style>');
$( "<style>.wrapper:after { border-top-width: " + value + "px; }</style>" ).appendTo( "head" )

Access the css ":after" selector with jQuery - Stack Overflow

jquery css jquery-selectors
Rectangle 27 38

You can add style for :after a like html code.For example:

var value = 22;
body.append('<style>.wrapper:after{border-top-width: ' + value + 'px;}</style>');
$( "<style>.wrapper:after { border-top-width: " + value + "px; }</style>" ).appendTo( "head" )

Access the css ":after" selector with jQuery - Stack Overflow

jquery css jquery-selectors
Rectangle 27 38

You can add style for :after a like html code.For example:

var value = 22;
body.append('<style>.wrapper:after{border-top-width: ' + value + 'px;}</style>');
$( "<style>.wrapper:after { border-top-width: " + value + "px; }</style>" ).appendTo( "head" )

Access the css ":after" selector with jQuery - Stack Overflow

jquery css jquery-selectors
Rectangle 27 38

You can add style for :after a like html code.For example:

var value = 22;
body.append('<style>.wrapper:after{border-top-width: ' + value + 'px;}</style>');
$( "<style>.wrapper:after { border-top-width: " + value + "px; }</style>" ).appendTo( "head" )

Access the css ":after" selector with jQuery - Stack Overflow

jquery css jquery-selectors
Rectangle 27 5

Use CSS stylesheet for solving this problem like the following code.

[divContainer_Id] img
{
    display:block;
    float:left;
    border:0;
}
<div id="[divContainer_Id]">
    <img src="[image1_url]" alt="img1" />
    <img src="[image2_url]" alt="img2" />
    <img src="[image3_url]" alt="img3" />
    <img src="[image4_url]" alt="img4" />
    <img src="[image5_url]" alt="img5" />
    <img src="[image6_url]" alt="img6" />
</div>

He said "row", not "column" and your HTML example is invalid.

He wants a single row with several images in 'columns'. This answer actually solves the problem, although "display: block" is redundant.

css - Removing whitespace between HTML elements when using line breaks...

html css image
Rectangle 27 5

Use CSS stylesheet for solving this problem like the following code.

[divContainer_Id] img
{
    display:block;
    float:left;
    border:0;
}
<div id="[divContainer_Id]">
    <img src="[image1_url]" alt="img1" />
    <img src="[image2_url]" alt="img2" />
    <img src="[image3_url]" alt="img3" />
    <img src="[image4_url]" alt="img4" />
    <img src="[image5_url]" alt="img5" />
    <img src="[image6_url]" alt="img6" />
</div>

He said "row", not "column" and your HTML example is invalid.

He wants a single row with several images in 'columns'. This answer actually solves the problem, although "display: block" is redundant.

css - Removing whitespace between HTML elements when using line breaks...

html css image