Javascript: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
Line 10: Line 10:


== Changing HTML Content ==
== Changing HTML Content ==
* Using jQuery [http://api.jquery.com/html/ .html()]<ref>[http://stackoverflow.com/questions/3563107/jquery-html-vs-innerhtml javascript - JQuery html() vs. innerHTML - Stack Overflow]</ref>
* Using jQuery [http://api.jquery.com/html/ .html()]<ref>[http://stackoverflow.com/questions/3563107/jquery-html-vs-innerhtml javascript - JQuery html() vs. innerHTML - Stack Overflow]</ref> <nowiki>$("#selected_id").html(htmlStr);</nowiki>
* [http://www.w3schools.com/js/js_htmldom_html.asp JavaScript HTML DOM Changing HTML Content]
* [http://www.w3schools.com/js/js_htmldom_html.asp JavaScript HTML DOM Changing HTML Content] <nowiki>document.getElementById(selected_id).innerHTML = htmlStr;</nowiki>


== Create HTML select option ==
== Create HTML select option ==

Revision as of 15:25, 16 February 2013

Javascript approaches to do the same task

Get the document by id

  • document.getElementById('abcId')
  • alternative: (1) Get the id attribute by using jQuery attr(): $("selector").attr("id");[1] (example) and (2) go to the first step
  • parallel naming rules for html elemnt tags ex: (1)<input id='checkbox_id1' > & <select id="select_id1"> (2)Replace the input id to select id. Code snippet as follows:
var selectId = inputId.replace("checkbox_", "select_");

Changing HTML Content

Create HTML select option

HTML select form Attribute

Display or hide the element by id

  • HTML DOM Style object
    • document.getElementById( selector_id ).style.display = "none";
  • Using jQuery .show() or .hide():
    • $("#selector_id").show(); or
    • $("#selector_id").hide();
  • Using jQuery .css():
    • $("#selector_id").css("display", ""); or $("#selector_id").css("display", "block");
    • $("#selector_id").css("display", "none");

Count the number of visible table rows

  • $('#selector_id:visible').length; jQuery v.1.9.1[3]
  • $('#selector_id:not([style*="display: none"])').length; Icon_exclaim.gif jQuery v.1.9.1 + IE Browser msie.png v.8 not work

count the number of all table rows (includes visible and invisible rows)

  • document.getElementById( table_id ).rows.length;

references