2
<button aria-hidden="true">Close</button>

I have a button tag in html. I want to add <data-dismiss="modal"> inside button tag by JQuery so that it become like <button data-dismiss="modal" aria-hidden="true">Close</button> . Is there any way to do it?

abhjt
  • 402
  • 4
  • 11
  • 25
  • 2
    Possible duplicate of [How to set data attributes in HTML elements](http://stackoverflow.com/questions/13524107/how-to-set-data-attributes-in-html-elements) – Mohammad Apr 07 '17 at 06:40

3 Answers3

2
$('button').attr("data-dismiss","modal");  

.attr takes 2 parameters, attribute and the value.

RonyLoud
  • 2,408
  • 2
  • 20
  • 25
haMzox
  • 2,073
  • 1
  • 12
  • 25
0

You can do this by using .attr() . First you set the attributeName, then the value

$('button').attr("data-dismiss","modal");
console.log($('button')[0])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button aria-hidden="true">Close</button>
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
0

here first of all there is no data-dismiss to the button tag, after clicking on open modal i appended data-dismiss to the button, try this

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg open-modal" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Enter Text</p>
          <input id="input_Text" type="text" class="form-control"/>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" >Close</button>
        </div>
      </div>
      
    </div>
  </div>
  
</div>
<script>
$('.open-modal').click(function(){
     $('button').attr("data-dismiss","modal");
});

$('#input_Text').change(function(){
if($('#input_Text').val()!=""){
 $('#myModal').modal('hide');
}
else{
  alert('Give some text');
}

});

</script>
</body>
</html>
Ganesh Putta
  • 2,622
  • 2
  • 19
  • 26
  • In that model window if i want to close it after entering any text value and if no text is enetered then it shouldnt close and ask for text. Then what modification need to be done. – abhjt Apr 07 '17 at 09:53