With the help of JQuery, you can get HTML values easily. In this tutorial, I will let you know how you can get attribute values. JQuery is a very simple but powerful Javascript plugin and widely used in web development.
How lớn install JQuery into your Project?
It is very simple you just need to download JQuery plugin on its official site or you can địa chỉ cửa hàng JQuery CDN that directly loads JQuery into your project. In this tutorial, I will use Bootstrap 4 & JQuery as a CDN.
Below snippet is bootstrap 4 snippets.
We can get HTML attribute values with the help of JQuery attr() function. You can also mix an attribute with the same function. And there is an HTML5 data attribute getting function also in JQuery data() that retrieves all data element values.
With the help of data() function you only able khổng lồ get data to lớn attribute values. But the help of attr() function you can get any attribute value lượt thích name, id, text & any other self define attribute values. The below code shows how you can use both functions.
Xem thêm: array_fill() trong php
$(document).ready(function(e) $("#userForm").on("submit",function()var user=$("#user").attr("data-user");var userId=$("#user").attr("data-user-id");console.log(user+" "+userId);var user=$("#user").attr("data-user");console.log(user);var data=$("#email").data();console.log(data);););
darkedeneurope.com”, userId: 1}
You can also get select option attribute values with the help of JQuery for that use below code.
$(document).ready(function(e) $("#userForm").on("submit",function()var city=$("#city option:selected").data();console.log(city);var state=$("#state option:selected").attr("data-state");console.log(state);););
Output:
city: 32You can also get all the values of any khung with the help of .serialize() the JQuery function.
$(document).ready(function(e) $("#userForm").on("submit",function()var fData = $(this).serialize(); console.log(fData);););
The above function will return all element values in the khung of string.
Output:
user=&email=&city=city3&state=state2
You can also set an attribute with the help of attr() function. attr(‘getAttribute’,’setAttribute’); the below example shows how you can phối attribute of an HTML element.
$(document).ready(function(e) $("#user").attr("userName","Test"); //Set attributevar user=$("#user").attr("userName"); //Get Attributeconsole.log(user););
Prev" class="btn btn-orange-dark btn-lg my-2" data-bs-toggle="tooltip" data-bs-placement="top" title="How lớn use Instascan an HTML5 QR scanner"> Prev " class="btn btn-orange-dark btn-lg my-2" data-bs-toggle="tooltip" data-bs-placement="top" title="Add or Remove array value with Javascript and JQuery">Next




Name

AJAXAPIarrayBootstrapBootstrap 4CSSDatabasedo while loopDropzoneelseif/else ifFacebookfgetcsvFontsfopenForeach LoopFor LoopFront kết thúc developmentHTMLHTML5JavaScriptJQueryJQuery PluginLets EncryptLoginLoopMobileAppMySqlMySqliPDOPhoneGapPHPPhpSpreadsheetReact JSReorderSnippetSortingSQLSQL ConstraintsSSLswitchtypescriptVue Jsweb developmentWhile LoopWordPress