Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
If there is duplicate attribute in the attr() then does it always apply last one duplicate attribute in jquery?

I am very confused only i want to know if i gave duplicate attribute in attr() then can i only apply last one attribute ex --> attr({style:'color:yellow',style:'font-family:arial',style:'color:yellow'}) I I have given three same style here can only apply last one? Is it possible to apply second one style?

<!DOCTYPE html>
<html>
<body>
   <p id="prg1">first paragraph</p>
</body>
<script src="C:\Users\SUDARSHAN\Desktop\html_UI\jquery-3.6.0.js">
</script>
<script>
   $('document').ready(function (){

      $('#prg1').attr({style:'color:yellow',style:'font-family:arial',style:'color:yellow',style:'border-style:dotted'});
    
   })
</script>
</html>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So, your question is a little bit confusing, but I will try to address all possible scenarios:

  1. Your code has a very bad error: you cannot have duplicate keys in objects, regardless of this being a style or a .attr() function. So this is completely wrong; any object b = { a: 1, a: 2 } is invalid and should not be used but the result will be b === { a: 2 }
  2. If you try to use .attr({style:'color:yellow'}).attr({style:'font-family:arial') yes, it will get the last one since the attr function always overwrite the value, never merges it;
  3. I think this is what you really want to know how to do: .attr({ style:'color:yellow; font-family:arial; border-style:dotted'}); see that every item on the style is separated by ';'. Basically, when you use .attr + style you are setting the complete style attribute for the html element, which means a whole css code so you don't need neither one 'style' per prop nor one 'attr' per prop; Simply set the entire css string to the style attribute, that said, you can have as many css props you want, just split them with ';'

EDIT: I've answered here based on your question, but as @Phil mentioned on the first comment below, you might get a better solution by using the .css() from jquery:

.css({
    color: "yellow", fontFamily: "arial", borderStyle: "dotted"
  });

Note the use of camelCase for the props with "-", or instead put the entire prop name among quotes like "border-style"

:)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!