Class Selector (“.class”) or ("#id") || Lấy dữ liệu bằng id hoặc class bằng jquery hoặc javascript (lấy value từ thẻ input, select option, textera)

Trước khi làm bất cứ thứ gì với jquery thì nên add cái file này vào không thì nó sẽ báo là 
{ is not function, not define ,....}
tìm đến thẻ head trong web và bỏ link này vào

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

-- Dùng jquery để thêm css cho thẻ div
<script>
$( ".myClass" ).css( "border", "3px solid red" );
</script>

-- Dùng jquey để lấy thuộc tính của selector 

jQuery( "[attribute*='value']" )
<input name="man-news">
<input name="milkman">
<input name="letterman2">
<input name="newmilk">

<script>
$( "input[name*='man']" ).val( "has man in it!" );
</script>

phần này thì trong thẻ input tất cả (*) thằng nào có name chứa
 chữ man thì thay thế value của nó là  " has man in it! "
-- Dùng jquery để lấy thuộc tính bắt đầu từ 1 chử nào đó 

[name^=”value”]
<input name="newsletter">
<input name="milkman">
<input name="newsboy"> 

<script>
$( "input[name^='news']" ).val( "news here!" );
</script>
phần này thì cái input nào bắt đầu là chử news thì thay bằng
" news here ! " chú ý cái dấu ^

-- Dùng jquery get value từ input tag
// First way to get a value
value = $("#txt_name").val(); 

// Second way to get a value
value = $("#txt_name").attr('value');

hoặc 1 cách nữa re chuột ra là lấy value liền
$(document).ready(function(){
  $("#txt_name").keyup(function(){
    alert(this.value);
  });
});

hoặc  this['inputname'].value
hoặc
<input type="text" id="txt_name" />
Get values from Input
// use to select with DOM element.
$("input").val();

// use the id to select the element.
$("#txt_name").val();

// use type="text" with input to select the element
$("input:text").val();
Set value to Input

// use to add "text content" to the DOM element.
$("input").val("text content");

// use the id to add "text content" to the element.
$("#txt_name").val("text content");

// use type="text" with input to add "text content" to the element
$("input:text").val("text content");

-- Dùng jquery để lấy data từ select option
<select id="selectVehicle">

       <option value="1" data-year="2011">Mazda</option>
       <option value="2" data-year="2015">Honda</option>
       <option value="3" data-year="2008">Mercedes</option>
       <option value="4" data-year="2005">Toyota</option>
  </select>

$("#selectVehicle").change(function () {
     alert($(this).find(':selected').data("year"));
});
-- Dùng jquey để thêm 1 class hoặc xóa cái class đi
$( "ul li" ).addClass(function( index ) {
  return "item-" + index;
});
xóa class
$( "p" ).removeClass( "myClass noClass" ).addClass( "yourClass" );
-- Dùng jquey add thêm attribute cho thẻ
$('#someid').attr('name', 'value');
hoặc là dựa vào id disabled 1 thứ ji đó
$('#someid').prop('disabled', true);
to remove it, use  removeProp()
$('#someid').removeProp('disabled');
//Phương thức prop () đặt hoặc trả về các thuộc tính và giá trị 
của các phần tử được chọn.Khi phương thức này được sử dụng để trả về
giá trị thuộc tính,nó sẽ trả về giá trị của phần tử khớp FIRST.
-->
Trả về giá trị của một thuộc tính:
$(selector).prop(property)
Đặt thuộc tính và giá trị:
$(selector).prop(property,value)
Đặt thuộc tính và giá trị bằng hàm:
$(selector).prop(property,function(index,currentvalue))
Đặt nhiều thuộc tính và giá trị:
$(selector).prop({property:value, property:value,...})
-- Dùng attr trong jquery để thêm thuộc tính bất kỳ
vào thẻ (alt, title, class, id, placeholder)
<img id="greatphoto" src="brush-seller.jpg" alt="brush seller">
$( "#greatphoto" ).attr( "alt", "Beijing Brush Seller" );
$( "#greatphoto" ).attr( "title", "Photo by Kelly Clark" );
hoặc là 
$( "#greatphoto" ).attr({
  alt: "Beijing Brush Seller",
  title: "photo by Kelly Clark"
});
-- Từ 1 thẻ có sẳn hoặc đang là ul li thêm 1 cái li thì jquery
có append()
<h2>Greetings</h2>
<div class="container">
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
</div>
$( ".inner" ).append( "<p>Test</p>" );
//kiếm tới cái class inner thêm 1 thẻ p

append() thêm 1 tag ở cuối 
còn muốn thêm ở đầu thì dùng .prepend()
từ 1 thẻ div có sẳn chơi thêm 2 thẻ trước sao 
var $newdiv1 = $( "<div id='object1'></div>" ),
  newdiv2 = document.createElement( "div" ),
  existingdiv1 = document.getElementById( "foo" );
$( "body" ).append( $newdiv1, [ newdiv2, existingdiv1 ] );

Comments

Bài đăn phổ biến

Tính Bình Phương 1 Số Trong Python

Deploy website lên VPS hoặc Hosting với Gitlab CI/CD

Đổi tên "READ MORE" trong bài post

MỞ CAMERA VÀ CHỤP ẢNH BẰNG PHP