Rectangle 27 69

$(event.target).id
$(event.target)[0].id

event.target.id also gives the id attribute.

this.id gives the id attribute.

and

$(this).id

The differences, of course, is between jQuery objects and DOM objects. "id" is a DOM function so you have to be on the DOM element object to use it.

event.target.id

@artaxerxe this means your clicked element doesn't have an id :P; also don't use jquery for this kind of things.... use event javascript attributes instead cause it's faster.

javascript - Getting the ID of the element that fired an event - Stack...

javascript jquery
Rectangle 27 69

$(event.target).id
$(event.target)[0].id

event.target.id also gives the id attribute.

this.id gives the id attribute.

and

$(this).id

The differences, of course, is between jQuery objects and DOM objects. "id" is a DOM function so you have to be on the DOM element object to use it.

event.target.id

@artaxerxe this means your clicked element doesn't have an id :P; also don't use jquery for this kind of things.... use event javascript attributes instead cause it's faster.

javascript - Getting the ID of the element that fired an event - Stack...

javascript jquery
Rectangle 27 30

To get the id attribute of a field, you would do:

To get the text contents of the a tags (the text between the opening and closing tags), you would do:

$('ul.formfield a').click(function() {
    var text = $(this).text();
    alert(text);
});

Please note the usage of $(this) inside the click function. You were re-using the selector which would not do what you want. Inside the event handler, this refers to the element being acted on, so with the code above you would get 'text' or 'textarea' depending on which one you clicked.

"this.id" would also have the value (it's standard for a DOM element)

How to get the id of an anchor tag in jQuery? - Stack Overflow

jquery
Rectangle 27 1212

You can use jQuery's attr() function. For example, if you img tag has an id attribute of 'my_image':

<img id="my_image" src="first.jpg"/>

Then you can change the src in jQuery by:

$("#my_image").attr("src","second.jpg");
click
$('#my_image').on({
    'click': function(){
        $('#my_image').attr('src','second.jpg');
    }
});

To rotate the image, you could do this:

$('img').on({
    'click': function() {
         var src = ($(this).attr('src') === 'img1_on.jpg')
            ? 'img2_on.jpg'
            : 'img1_on.jpg';
         $(this).attr('src', src);
    }
});

@jonstjohn Thanks, I learned something new. So in the case above, we want to be sure that result of .attr('src') be string. Is there some other possibility also?

@Farhan I consider it good practice to use strict comparison when you know what type you are expecting. We expect .attr('src') to be a string, so strict makes sense. Other folks may have different opinions no the matter.

What if image don't have ID/Class assign to it (as shown in question) and we can't even add, then in that case how will we change src?

javascript - Changing the image source using jQuery - Stack Overflow

javascript jquery image
Rectangle 27 133

First give your form an id attribute, then use code like this:

$(document).ready( function() {
  var form = $('#my_awesome_form');

  form.find('select:first').change( function() {
    $.ajax( {
      type: "POST",
      url: form.attr( 'action' ),
      data: form.serialize(),
      success: function( response ) {
        console.log( response );
      }
    } );
  } );

} );

So this code uses .serialize() to pull out the relevant data from the form. It also assumes the select you care about is the first one in the form.

For future reference, the jQuery docs are very, very good.

A very important point, that may be completely obvious to many but was definitely not so to me which is omitted here and in all the other SO posts on this subject is that in order to be successful you must ensure that the button you are using to trigger the event that causes the submit of the form via ajax IS NOT type submit! Else this will always fail. The serialize approach here is neat.

Since this code handles submitting the form on change of a select box, it doesn't matter if you have <input type='submit' /> in the form. If you do have a submit button in the form you simply need to handle the submit event on the form instead and prevent the default action. Once again the docs come to the rescue on this :)

Submit form using AJAX and jQuery - Stack Overflow

jquery ajax serialization
Rectangle 27 129

First give your form an id attribute, then use code like this:

$(document).ready( function() {
  var form = $('#my_awesome_form');

  form.find('select:first').change( function() {
    $.ajax( {
      type: "POST",
      url: form.attr( 'action' ),
      data: form.serialize(),
      success: function( response ) {
        console.log( response );
      }
    } );
  } );

} );

So this code uses .serialize() to pull out the relevant data from the form. It also assumes the select you care about is the first one in the form.

For future reference, the jQuery docs are very, very good.

A very important point, that may be completely obvious to many but was definitely not so to me which is omitted here and in all the other SO posts on this subject is that in order to be successful you must ensure that the button you are using to trigger the event that causes the submit of the form via ajax IS NOT type submit! Else this will always fail. The serialize approach here is neat.

Since this code handles submitting the form on change of a select box, it doesn't matter if you have <input type='submit' /> in the form. If you do have a submit button in the form you simply need to handle the submit event on the form instead and prevent the default action. Once again the docs come to the rescue on this :)

Submit form using AJAX and jQuery - Stack Overflow

jquery ajax serialization
Rectangle 27 182

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

HTML 5 is even more permissive, saying only that an id must contain at least one character and may not contain any space characters.

A common mistake is to use an ID that starts with a digit.

The id attribute is case sensitive in XHTML.

IE6 didn't support ID starting with underscores, but it's dead anyway.

As a purely practical matter, you may want to avoid certain characters. Periods, colons and '#' have special meaning in CSS selectors, so you will have to escape those characters using a backslash in CSS or a double backslash in a selector string passed to jQuery. Think about how often you will have to escape a character in your stylesheets or code before you go crazy with periods and colons in ids.

@rahmanisback regarding IE6, one would have thought so, but I'm finishing up a proposal right now for a bank and they insist that any application a vendor develops runs in IE6. This is for 30,000 users. Heck, if we could just get them to update their browsers on all those desktops, it might just help the unemployment rate.

@Karl I'm sorry to hear this. Do all of your efforts to warn about IE6 security issues. However IE7 will soon be the new IE6, so yeah it appears to be our fate in this industry to remedy MS past mistakes.

For example, the HTML declaration <div id="first.name"></div> is valid. You can select that element in CSS as #first\.name and in jQuery like so: $('#first\\.name'). But if you forget the backslash, $('#first.name'), you will have a perfectly valid selector looking for an element with id first and also having class name. This is a bug that is easy to overlook. You might be happier in the long run choosing the id first-name (a hyphen rather than a period), instead.

@MrShark The second link is broken; New link

You can simplify your development tasks by strictly sticking to a naming convention. For example, if you limit yourself entirely to lower-case characters and always separate words with either hyphens or underscores (but not both, pick one and never use the other), then you have an easy-to-remember pattern. You will never wonder "was it firstName or FirstName?" because you will always know that you should type first_name. Prefer camel case? Then limit yourself to that, no hyphens or underscores, and always, consistently use either upper-case or lower-case for the first character, don't mix them.

A now very obscure problem was that at least one browser, Netscape 6, incorrectly treated id attribute values as case-sensitive. That meant that if you had typed id="firstName" in your HTML (lower-case 'f') and #FirstName { color: red } in your CSS (upper-case 'F'), that buggy browser would have failed to set the element's color to red. At the time of this edit, April 2015, I hope you aren't being asked to support Netscape 6. Consider this a historical footnote.

Note that class and id attributes are case-sensitive in XHTML, all other attributes are not. Eric Meyer mentioned this in a CSS workshop I attended.

Also note that if you try to write a CSS rule to target an element by ID, and the ID beings with a number, it won't work. Bummer!

As for '.' or ':' in an ID using jQuery, see the jQuery FAQ. It contains a small function that does the necessary escaping.

The id attribute is [w3.org/TR/html4/struct/global.html#adef-id](case sensitive in HTML4) and has to begin with a letter (limited to A to Z). Also note that your example should not make your element's text color red since your CSS refers to an element with class FirstName not to your id.

The error that Augustus pointed out is still present. In your example, if you have id="firstName" in either HTML 4 or HTML 5 and #FirstName { color: red } in CSS, only a buggy browser will set the element's color to red.

What are valid values for the id attribute in HTML? - Stack Overflow

html
Rectangle 27 182

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

HTML 5 is even more permissive, saying only that an id must contain at least one character and may not contain any space characters.

A common mistake is to use an ID that starts with a digit.

The id attribute is case sensitive in XHTML.

IE6 didn't support ID starting with underscores, but it's dead anyway.

As a purely practical matter, you may want to avoid certain characters. Periods, colons and '#' have special meaning in CSS selectors, so you will have to escape those characters using a backslash in CSS or a double backslash in a selector string passed to jQuery. Think about how often you will have to escape a character in your stylesheets or code before you go crazy with periods and colons in ids.

@rahmanisback regarding IE6, one would have thought so, but I'm finishing up a proposal right now for a bank and they insist that any application a vendor develops runs in IE6. This is for 30,000 users. Heck, if we could just get them to update their browsers on all those desktops, it might just help the unemployment rate.

@Karl I'm sorry to hear this. Do all of your efforts to warn about IE6 security issues. However IE7 will soon be the new IE6, so yeah it appears to be our fate in this industry to remedy MS past mistakes.

For example, the HTML declaration <div id="first.name"></div> is valid. You can select that element in CSS as #first\.name and in jQuery like so: $('#first\\.name'). But if you forget the backslash, $('#first.name'), you will have a perfectly valid selector looking for an element with id first and also having class name. This is a bug that is easy to overlook. You might be happier in the long run choosing the id first-name (a hyphen rather than a period), instead.

@MrShark The second link is broken; New link

You can simplify your development tasks by strictly sticking to a naming convention. For example, if you limit yourself entirely to lower-case characters and always separate words with either hyphens or underscores (but not both, pick one and never use the other), then you have an easy-to-remember pattern. You will never wonder "was it firstName or FirstName?" because you will always know that you should type first_name. Prefer camel case? Then limit yourself to that, no hyphens or underscores, and always, consistently use either upper-case or lower-case for the first character, don't mix them.

A now very obscure problem was that at least one browser, Netscape 6, incorrectly treated id attribute values as case-sensitive. That meant that if you had typed id="firstName" in your HTML (lower-case 'f') and #FirstName { color: red } in your CSS (upper-case 'F'), that buggy browser would have failed to set the element's color to red. At the time of this edit, April 2015, I hope you aren't being asked to support Netscape 6. Consider this a historical footnote.

Note that class and id attributes are case-sensitive in XHTML, all other attributes are not. Eric Meyer mentioned this in a CSS workshop I attended.

Also note that if you try to write a CSS rule to target an element by ID, and the ID beings with a number, it won't work. Bummer!

As for '.' or ':' in an ID using jQuery, see the jQuery FAQ. It contains a small function that does the necessary escaping.

The id attribute is [w3.org/TR/html4/struct/global.html#adef-id](case sensitive in HTML4) and has to begin with a letter (limited to A to Z). Also note that your example should not make your element's text color red since your CSS refers to an element with class FirstName not to your id.

The error that Augustus pointed out is still present. In your example, if you have id="firstName" in either HTML 4 or HTML 5 and #FirstName { color: red } in CSS, only a buggy browser will set the element's color to red.

What are valid values for the id attribute in HTML? - Stack Overflow

html
Rectangle 27 304

ngAttr directive can totally be of help here, as introduced in the official documentation

For instance, to set the id attribute value of a div element, so that it contains an index, a view fragment might contain

<div ng-attr-id="{{ 'object-' + myScopeObject.index }}"></div>

which would get interpolated to

<div id="object-1"></div>
myScopeObject

@JanAagaard Let us assume myScopeObject is a property of a scope object exposed using a controller. Please see also docs.angularjs.org/guide/controller. Is that clear enough for you or shall I elaborate further?

i did ng-attr-id="{{ 'Panel' + file.Id }}" but it does not generate id="Panel12312" for me :(

Aren't the following two identical in behavior: <div id="{{ 'object' + index }}"> and <div ng-attr-id="{{ 'object' + index }}"> ? The docs seem to say prepending ng-attr- is to help out for cases where the element is something non-standard, like not a <div>. Am I reading the docs right?

@broc.seib using nd-attr is not only about standard. This is good practice because HTML interpreter can assign id to the element before angular gets loaded. And ng-attr ensures to assign id to the element only when angular gets loaded. same is the case for ng-src in <img> tag.

How to set the id attribute of a HTML element dynamically with angular...

angularjs angularjs-scope concatenation string-concatenation
Rectangle 27 182

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

A common mistake is to use an ID that starts with a digit.

A common mistake is to use an ID that starts with a digit.

HTML 5 is even more permissive, saying only that an id must contain at least one character and may not contain any space characters.

HTML 5 is even more permissive, saying only that an id must contain at least one character and may not contain any space characters.

IE6 didn't support ID starting with underscores, but it's dead anyway.

@rahmanisback regarding IE6, one would have thought so, but I'm finishing up a proposal right now for a bank and they insist that any application a vendor develops runs in IE6. This is for 30,000 users. Heck, if we could just get them to update their browsers on all those desktops, it might just help the unemployment rate.

The id attribute is case sensitive in XHTML [citation needed].

The id attribute is case sensitive in XHTML.

@Karl I'm sorry to hear this. Do all of your efforts to warn about IE6 security issues. However IE7 will soon be the new IE6, so yeah it appears to be our fate in this industry to remedy MS past mistakes.

As a purely practical matter, you may want to avoid certain characters. Periods, colons and '#' have special meaning in CSS selectors, so you will have to escape those characters using a backslash in CSS or a double backslash in a selector string passed to jQuery. Think about how often you will have to escape a character in your stylesheets or code before you go crazy with periods and colons in ids.

@rahmanisback regarding IE6, one would have thought so, but I'm finishing up a proposal right now for a bank and they insist that any application a vendor develops runs in IE6. This is for 30,000 users. Heck, if we could just get them to update their browsers on all those desktops, it might just help the unemployment rate.

As a purely practical matter, you may want to avoid certain characters. Periods, colons and '#' have special meaning in CSS selectors, so you will have to escape those characters using a backslash in CSS or a double backslash in a selector string passed to jQuery. Think about how often you will have to escape a character in your stylesheets or code before you go crazy with periods and colons in ids.

For example, the HTML declaration <div id="first.name"></div> is valid. You can select that element in CSS as #first\.name and in jQuery like so: $('#first\\.name'). But if you forget the backslash, $('#first.name'), you will have a perfectly valid selector looking for an element with id first and also having class name. This is a bug that is easy to overlook. You might be happier in the long run choosing the id first-name (a hyphen rather than a period), instead.

@Karl I'm sorry to hear this. Do all of your efforts to warn about IE6 security issues. However IE7 will soon be the new IE6, so yeah it appears to be our fate in this industry to remedy MS past mistakes.

For example, the HTML declaration <div id="first.name"></div> is valid. You can select that element in CSS as #first\.name and in jQuery like so: $('#first\\.name'). But if you forget the backslash, $('#first.name'), you will have a perfectly valid selector looking for an element with id first and also having class name. This is a bug that is easy to overlook. You might be happier in the long run choosing the id first-name (a hyphen rather than a period), instead.

@MrShark The second link is broken; New link

You can simplify your development tasks by strictly sticking to a naming convention. For example, if you limit yourself entirely to lower-case characters and always separate words with either hyphens or underscores (but not both, pick one and never use the other), then you have an easy-to-remember pattern. You will never wonder "was it firstName or FirstName?" because you will always know that you should type first_name. Prefer camel case? Then limit yourself to that, no hyphens or underscores, and always, consistently use either upper-case or lower-case for the first character, don't mix them.

You can simplify your development tasks by strictly sticking to a naming convention. For example, if you limit yourself entirely to lower-case characters and always separate words with either hyphens or underscores (but not both, pick one and never use the other), then you have an easy-to-remember pattern. You will never wonder "was it firstName or FirstName?" because you will always know that you should type first_name. Prefer camel case? Then limit yourself to that, no hyphens or underscores, and always, consistently use either upper-case or lower-case for the first character, don't mix them.

A now very obscure problem was that at least one browser, Netscape 6, incorrectly treated id attribute values as case-sensitive. That meant that if you had typed id="firstName" in your HTML (lower-case 'f') and #FirstName { color: red } in your CSS (upper-case 'F'), that buggy browser would have failed to set the element's color to red. At the time of this edit, April 2015, I hope you aren't being asked to support Netscape 6. Consider this a historical footnote.

A now very obscure problem was that at least one browser, Netscape 6, incorrectly treated id attribute values as case-sensitive. That meant that if you had typed id="firstName" in your HTML (lower-case 'f') and #FirstName { color: red } in your CSS (upper-case 'F'), that buggy browser would have failed to set the element's color to red. At the time of this edit, April 2015, I hope you aren't being asked to support Netscape 6. Consider this a historical footnote.

Note that class and id attributes are case-sensitive in XHTML, all other attributes are not. Eric Meyer mentioned this in a CSS workshop I attended.

Note that class and id attributes are case-sensitive in XHTML, all other attributes are not. Eric Meyer mentioned this in a CSS workshop I attended.

Also note that if you try to write a CSS rule to target an element by ID, and the ID beings with a number, it won't work. Bummer!

Also note that if you try to write a CSS rule to target an element by ID, and the ID beings with a number, it won't work. Bummer!

As for '.' or ':' in an ID using jQuery, see the jQuery FAQ. It contains a small function that does the necessary escaping.

As for '.' or ':' in an ID using jQuery, see the jQuery FAQ. It contains a small function that does the necessary escaping.

Note that HTML5 allows much more then HTML4, see for example 456bereastreet.com/archive/201011/… and w3.org/TR/html5/elements.html#the-id-attribute

The id attribute is [w3.org/TR/html4/struct/global.html#adef-id](case sensitive in HTML4) and has to begin with a letter (limited to A to Z). Also note that your example should not make your element's text color red since your CSS refers to an element with class FirstName not to your id.

The id attribute is [w3.org/TR/html4/struct/global.html#adef-id](case sensitive in HTML4) and has to begin with a letter (limited to A to Z). Also note that your example should not make your element's text color red since your CSS refers to an element with class FirstName not to your id.

The error that Augustus pointed out is still present. In your example, if you have id="firstName" in either HTML 4 or HTML 5 and #FirstName { color: red } in CSS, only a buggy browser will set the element's color to red.

What are valid values for the id attribute in HTML? - Stack Overflow

html
Rectangle 27 119

You can technically use colons and periods in id/name attributes, but I would strongly suggest avoiding both.

You can technically use colons and periods in id/name attributes, but I would strongly suggest avoiding both.

In CSS (and several JavaScript libraries like jQuery), both the period and the colon have special meaning and you will run into problems if you're not careful. Periods are class selectors and colons are pseudo-selectors (eg., ":hover" for an element when the mouse is over it).

In CSS (and several JavaScript libraries like jQuery), both the period and the colon have special meaning and you will run into problems if you're not careful. Periods are class selectors and colons are pseudo-selectors (eg., ":hover" for an element when the mouse is over it).

#my.cool:thing { ... /* some rules */ ... }
#my.cool:thing { ... /* some rules */ ... }

Which is really saying, "the element with an id of 'my', a class of 'cool' and the 'thing' pseudo-selector" in CSS-speak.

Which is really saying, "the element with an id of 'my', a class of 'cool' and the 'thing' pseudo-selector" in CSS-speak.

Stick to A-Z of any case, numbers, underscores and hyphens. And as said above, make sure your ids are unique.

Stick to A-Z of any case, numbers, underscores and hyphens. And as said above, make sure your ids are unique.

You can use colons and periods - but you'll need to escape them using double backslashes, eg: $('#my\\.cool\\:thing') or escaping a variable: $('#'+id.replace(/\./,\\.).replace(/\:/,\\:)) groups.google.com/group/jquery-en/browse_thread/thread/

You can use colons and periods - but you'll need to escape them using double backslashes, eg: $('#my\\.cool\\:thing') or escaping a variable: $('#'+id.replace(/\./,’\\.’).replace(/\:/,’\\:’)) groups.google.com/group/jquery-en/browse_thread/thread/…

Why not numerals; why just A-Z? Numbers are very useful IDs when referring to elements that are related to data that's keyed with a number, as long as you don't start with the number.

Why not numerals; why just A-Z? Numbers are very useful IDs when referring to elements that are related to data that's keyed with a number, as long as you don't start with the number.

@jowformd: interesting idea using replace in the selector, instead of using the function twice, why not just improve the regex id.replace(/([\.:])/g,"\\$1")

@jowformd: interesting idea using replace in the selector, instead of using the function twice, why not just improve the regex id.replace(/([\.:])/g,"\\$1")

If you have these characters (., :) in ids, and cannot remove them (cough ... Sharepoint), you can get around this in CSS with attribute selectors instead of id selectors, e.g. [id='my.cool:thing'], however this selector will have a lower specificity than an id selector, which might cause other problems.

If you have these characters (., :) in ids, and cannot remove them (cough ... Sharepoint), you can get around this in CSS with attribute selectors instead of id selectors, e.g. [id='my.cool:thing'], however this selector will have a lower specificity than an id selector, which might cause other problems.

What are valid values for the id attribute in HTML? - Stack Overflow

html
Rectangle 27 119

You can technically use colons and periods in id/name attributes, but I would strongly suggest avoiding both.

In CSS (and several JavaScript libraries like jQuery), both the period and the colon have special meaning and you will run into problems if you're not careful. Periods are class selectors and colons are pseudo-selectors (eg., ":hover" for an element when the mouse is over it).

#my.cool:thing { ... /* some rules */ ... }

Which is really saying, "the element with an id of 'my', a class of 'cool' and the 'thing' pseudo-selector" in CSS-speak.

Stick to A-Z of any case, numbers, underscores and hyphens. And as said above, make sure your ids are unique.

You can use colons and periods - but you'll need to escape them using double backslashes, eg: $('#my\\.cool\\:thing') or escaping a variable: $('#'+id.replace(/\./,\\.).replace(/\:/,\\:)) groups.google.com/group/jquery-en/browse_thread/thread/

Why not numerals; why just A-Z? Numbers are very useful IDs when referring to elements that are related to data that's keyed with a number, as long as you don't start with the number.

@jowformd: interesting idea using replace in the selector, instead of using the function twice, why not just improve the regex id.replace(/([\.:])/g,"\\$1")

If you have these characters (., :) in ids, and cannot remove them (cough ... Sharepoint), you can get around this in CSS with attribute selectors instead of id selectors, e.g. [id='my.cool:thing'], however this selector will have a lower specificity than an id selector, which might cause other problems.

What are valid values for the id attribute in HTML? - Stack Overflow

html
Rectangle 27 119

You can technically use colons and periods in id/name attributes, but I would strongly suggest avoiding both.

In CSS (and several JavaScript libraries like jQuery), both the period and the colon have special meaning and you will run into problems if you're not careful. Periods are class selectors and colons are pseudo-selectors (eg., ":hover" for an element when the mouse is over it).

#my.cool:thing { ... /* some rules */ ... }

Which is really saying, "the element with an id of 'my', a class of 'cool' and the 'thing' pseudo-selector" in CSS-speak.

Stick to A-Z of any case, numbers, underscores and hyphens. And as said above, make sure your ids are unique.

You can use colons and periods - but you'll need to escape them using double backslashes, eg: $('#my\\.cool\\:thing') or escaping a variable: $('#'+id.replace(/\./,\\.).replace(/\:/,\\:)) groups.google.com/group/jquery-en/browse_thread/thread/

Why not numerals; why just A-Z? Numbers are very useful IDs when referring to elements that are related to data that's keyed with a number, as long as you don't start with the number.

@jowformd: interesting idea using replace in the selector, instead of using the function twice, why not just improve the regex id.replace(/([\.:])/g,"\\$1")

If you have these characters (., :) in ids, and cannot remove them (cough ... Sharepoint), you can get around this in CSS with attribute selectors instead of id selectors, e.g. [id='my.cool:thing'], however this selector will have a lower specificity than an id selector, which might cause other problems.

What are valid values for the id attribute in HTML? - Stack Overflow

html
Rectangle 27 30

(I think the id attribute needs to start with a letter. Could be wrong.)

You could go for event delegation...

<div onClick="reply_click()">
    <button id="1"></button>
    <button id="2"></button>
    <button id="3"></button>
</div>

function reply_click(e) {
    e = e || window.event;
    e = e.target || e.srcElement;
    if (e.nodeName === 'BUTTON') {
        alert(e.id);
    }
}

...but that requires you to be relatively comfortable with the wacky event model.

This won't work. You've specified code in the onclick attribute that calls reply_click with no arguments. So, no event argument will be passed to the function.

And the value of the "id" attribute can be any string. It doesn't have to start with a letter.

@Jason Actually, in all the good modern browsers, the e argument is generated automatically. If it isn't, then we must be dealing with IE6-8, which instead provides that useful object via window.event.

@sdleihssirhc Actually, you arrogant so-and-so, that all changes with HTML5.

html - JavaScript - onClick to get the ID of the clicked button - Stac...

javascript html
Rectangle 27 30

(I think the id attribute needs to start with a letter. Could be wrong.)

You could go for event delegation...

<div onClick="reply_click()">
    <button id="1"></button>
    <button id="2"></button>
    <button id="3"></button>
</div>

function reply_click(e) {
    e = e || window.event;
    e = e.target || e.srcElement;
    if (e.nodeName === 'BUTTON') {
        alert(e.id);
    }
}

...but that requires you to be relatively comfortable with the wacky event model.

This won't work. You've specified code in the onclick attribute that calls reply_click with no arguments. So, no event argument will be passed to the function.

And the value of the "id" attribute can be any string. It doesn't have to start with a letter.

@Jason Actually, in all the good modern browsers, the e argument is generated automatically. If it isn't, then we must be dealing with IE6-8, which instead provides that useful object via window.event.

@sdleihssirhc Actually, you arrogant so-and-so, that all changes with HTML5.

html - JavaScript - onClick to get the ID of the clicked button - Stac...

javascript html
Rectangle 27 34

gets rid of the additional restrictions on the id attribute see here. The only requirements left (apart from being unique in the document) are:

[A-Za-z][-A-Za-z0-9_:.]*
  • Must Start with A-Z or a-z characters

but one should avoid : and . beacause:

For example, an ID could be labelled "a.b:c" and referenced in the style sheet as #a.b:c but as well as being the id for the element, it could mean id "a", class "b", pseudo-selector "c". Best to avoid the confusion and stay away from using . and : altogether.

What are valid values for the id attribute in HTML? - Stack Overflow

html
Rectangle 27 34

gets rid of the additional restrictions on the id attribute see here. The only requirements left (apart from being unique in the document) are:

[A-Za-z][-A-Za-z0-9_:.]*
  • Must Start with A-Z or a-z characters

but one should avoid : and . beacause:

For example, an ID could be labelled "a.b:c" and referenced in the style sheet as #a.b:c but as well as being the id for the element, it could mean id "a", class "b", pseudo-selector "c". Best to avoid the confusion and stay away from using . and : altogether.

What are valid values for the id attribute in HTML? - Stack Overflow

html
Rectangle 27 34

gets rid of the additional restrictions on the id attribute see here. The only requirements left (apart from being unique in the document) are:

gets rid of the additional restrictions on the id attribute see here. The only requirements left (apart from being unique in the document) are:

[A-Za-z][-A-Za-z0-9_:.]*
[A-Za-z][-A-Za-z0-9_:.]*
  • Must Start with A-Z or a-z characters
  • Must Start with A-Z or a-z characters

but one should avoid : and . beacause:

but one should avoid : and . beacause:

For example, an ID could be labelled "a.b:c" and referenced in the style sheet as #a.b:c but as well as being the id for the element, it could mean id "a", class "b", pseudo-selector "c". Best to avoid the confusion and stay away from using . and : altogether.

For example, an ID could be labelled "a.b:c" and referenced in the style sheet as #a.b:c but as well as being the id for the element, it could mean id "a", class "b", pseudo-selector "c". Best to avoid the confusion and stay away from using . and : altogether.

What are valid values for the id attribute in HTML? - Stack Overflow

html
Rectangle 27 456

Note on self-closing tags and id= versus name=

Take me to [pookie](#pookie)
Take me to [pookie](#pookie)

should be the correct markdown syntax to jump to the anchor point named pookie.

should be the correct markdown syntax to jump to the anchor point named pookie.

To insert an anchor point of that name use HTML:

To insert an anchor point of that name use HTML:

<a name="pookie"></a>
<a name="pookie"></a>

Markdown doesn't seem to mind where you put the anchor point. A useful place to put it is in a header. For example:

[Update: use name= instead of id=. See Note below.]

Markdown doesn't seem to mind where you put the anchor point. A useful place to put it is in a header. For example:

### <a name="tith"></a>This is the Heading
### <a name="tith"></a>This is the Heading

works very well. (I'd demonstrate here but SO's renderer strips out the anchor.)

works very well. (I'd demonstrate here but SO's renderer strips out the anchor.)

An earlier version of this post suggested using <a id='tith' />, using the self-closing syntax for XHTML, and using the id attribute instead of name.

XHTML allowed any tag to be 'empty' and 'self-closed'. That is, <tag /> is short-hand for an empty body and a closing tag <tag></tag>. Most browsers will accept XHTML, but some do not. To avoid this problem close the tag explicitly, as I recommend above. (That is, avoid the self-closing form <a name="identitifer" />.

XHTML allows for any tag to be 'empty' and 'self-closed'. That is, <tag /> is short-hand for <tag></tag>, a matched pair of tags with an empty body. Most browsers will accept XHTML, but some do not. To avoid cross-browser problems, close the tag explicitly using <tag></tag>, as recommended above.

Finally, the attribute name= was deprecated in XHTML, so I originally used id=, which everyone recognises. However, HTML5 now creates a global variable in JavaScript when using id=, and this may not necessarily be what you want. So, using name= is now likely to be more friendly.

Finally, the attribute name= was deprecated in XHTML, so I originally used id=, which everyone recognises. But HTML5 creates JavaScript globals when using id=, and this is not necessarily what you want, so using name= is now likely to be more friendly.

(Thanks to Slipp Douglas for explaining XHTML to me, and nailer for pointing out the HTML5 side-effect — see the comments and nailer's answer for more detail. name= appears to work everywhere, though it is deprecated in XHTML.)

(Thanks to Slipp Douglas for explaining XHTML to me, and nailer for pointing out the HTML5 side-effect see the comments and nailer's answer for more detail. name= appears to work everywhere, though it is deprecated in XHTML.)

You can't see how to link to your heading demo after StackOverflow renders the HTML because their rendered is stripping out your <a> tag. That is, you can't in StackOverflow Markdown.

However, this will work in other, more-liberal Markdown renderers, but you'll need a closing <a> tag; the <a> tag doesn't allow self-closing. Also, I found my browser to skip past the header unless the <a> tag is before the header's contents. Corrections made to your examples.

Slipp: Thanks for the explanation. Self-closing <a> tags work fine for me. Can you give a reference?

However, this will work in other, more-liberal Markdown renderers, but you'll need a closing <a> tag; the <a> tag doesn't allow self-closing. Also, I found my browser to skip past the header unless the <a> tag is before the header's contents. Corrections made to your examples.

Hold up there, cowboy. Just because you don't have any styling on an <a> without an href doesn't mean that it's self closing. Unless I'm completely farking insane, both of these: test-xhtml11 and [sln.6bitt.com/public/test-html5.html](test-html5) render the rest of the page within the <a> tag. Go ahead and inspect with a web inspector of your choice.

Hold up there, cowboy. Just because you don't have any styling on an <a> without an href doesn't mean that it's self closing. Unless I'm completely farking insane, both of these: test-xhtml11 and [sln.6bitt.com/public/test-html5.html](test-html5) render the rest of the page within the <a> tag. Go ahead and inspect with a web inspector of your choice.

@Slipp: OK I think I understand, now. You coded <a id="hi"/> rest of doc, but it was treated like <a id="hi"> rest of doc</a>. (And the element analysis of the page shows this, too.) My mistake: I looked at the elements displayed not the raw source. Do you think the answer should be modified, in light of this observation?

@Slipp: OK I think I understand, now. You coded <a id="hi"/> rest of doc, but it was treated like <a id="hi"> rest of doc</a>. (And the element analysis of the page shows this, too.) My mistake: I looked at the elements displayed not the raw source. Do you think the answer should be modified, in light of this observation?

The name attribute also creates global variables (see stackoverflow.com/questions/3434278/), so you might as well use the id attribute as the target of fragment identifier URLs, as intended.

html - Cross-reference (named anchor) in markdown - Stack Overflow

html anchor markdown cross-reference
Rectangle 27 448

Note on self-closing tags and id= versus name=

Take me to [pookie](#pookie)

should be the correct markdown syntax to jump to the anchor point named pookie.

To insert an anchor point of that name use HTML:

<a name="pookie"></a>

Markdown doesn't seem to mind where you put the anchor point. A useful place to put it is in a header. For example:

### <a name="tith"></a>This is the Heading

works very well. (I'd demonstrate here but SO's renderer strips out the anchor.)

An earlier version of this post suggested using <a id='tith' />, using the self-closing syntax for XHTML, and using the id attribute instead of name.

XHTML allows for any tag to be 'empty' and 'self-closed'. That is, <tag /> is short-hand for <tag></tag>, a matched pair of tags with an empty body. Most browsers will accept XHTML, but some do not. To avoid cross-browser problems, close the tag explicitly using <tag></tag>, as recommended above.

Finally, the attribute name= was deprecated in XHTML, so I originally used id=, which everyone recognises. However, HTML5 now creates a global variable in JavaScript when using id=, and this may not necessarily be what you want. So, using name= is now likely to be more friendly.

(Thanks to Slipp Douglas for explaining XHTML to me, and nailer for pointing out the HTML5 side-effect see the comments and nailer's answer for more detail. name= appears to work everywhere, though it is deprecated in XHTML.)

You can't see how to link to your heading demo after StackOverflow renders the HTML because their rendered is stripping out your <a> tag. That is, you can't in StackOverflow Markdown.

However, this will work in other, more-liberal Markdown renderers, but you'll need a closing <a> tag; the <a> tag doesn't allow self-closing. Also, I found my browser to skip past the header unless the <a> tag is before the header's contents. Corrections made to your examples.

Hold up there, cowboy. Just because you don't have any styling on an <a> without an href doesn't mean that it's self closing. Unless I'm completely farking insane, both of these: test-xhtml11 and [sln.6bitt.com/public/test-html5.html](test-html5) render the rest of the page within the <a> tag. Go ahead and inspect with a web inspector of your choice.

@Slipp: OK I think I understand, now. You coded <a id="hi"/> rest of doc, but it was treated like <a id="hi"> rest of doc</a>. (And the element analysis of the page shows this, too.) My mistake: I looked at the elements displayed not the raw source. Do you think the answer should be modified, in light of this observation?

The name attribute also creates global variables (see stackoverflow.com/questions/3434278/), so you might as well use the id attribute as the target of fragment identifier URLs, as intended.

html - Cross-reference (named anchor) in markdown - Stack Overflow

html anchor markdown cross-reference