0

I tried to display XML data to popup box. Now It displays all data of XML file data on the popup. But Actually, When I click open modal1, need to load only the first data set(array-0) from an XML file. Also when clicking open modal2, need to load only the second data set(array-1) from XML.How can I do it using js loop?

This is my code.

data.xml

<?xml version="1.0"?>
<paintings>
  <painting>
    <title>test</title>
    <artist>Test Artist</artist>
    <image>https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Liliumbulbiferumflowertop.jpg/220px-Liliumbulbiferumflowertop.jpg</image>
    <price>850</price>
  </painting>

  <painting>
    <title>test2</title>
    <artist>Test Artist2</artist>
    <image>https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Liliumbulbiferumflowertop.jpg/220px-Liliumbulbiferumflowertop.jpg</image>
    <price>8503</price>
  </painting>
</paintings>

index.html

<div class="container">


<a href="#openModal1" id="openModalBtn">open modal1</a>
<a href="#openModal2" id="openModalBtn">open modal2</a>


<div id="openModal1" class="modalDialog">
    <div>
       <input class="getAssignment2" type="button" value="Previous" id="prev">
        <input class="getAssignment" type="button" value="Next" id="next">
    <a href="#close" title="Close" class="close">X</a>
   <div id="container"></div>
   </div>
</div>

</div>

JavaScript code

$(document).ready(function(){
        $.ajax({
            type:"GET",
            url:"data.xml",
            dataType:"xml",
            success:xmlParser
        });
    });

    function xmlParser(xml){
        $(xml).find("painting").each(function(){
            $("#container").append(
                '<div class="painting"><img src="' + $(this).find("image").text() + '" width="200" height="225" alt="' + $(this).find("title").text() + '" /> <br/> <div class="title">' + $(this).find("title").text() + '<br/>$' + $(this).find("price").text() + '</div></div>');

        });
    }

1 Answer 1

0

The below code can help you

$('.modal-click').click(function(){
		xmlParser(xml,this.id);
	})
	var xml = '<paintings><painting><title>test</title><artist>Test Artist</artist><image>https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Liliumbulbiferumflowertop.jpg/220px-Liliumbulbiferumflowertop.jpg</image><price>850</price></painting><painting><title>test2</title><artist>Test Artist2</artist><image>https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Liliumbulbiferumflowertop.jpg/220px-Liliumbulbiferumflowertop.jpg</image><price>8503</price></painting></paintings>'
	    function xmlParser(xml,id){
	        	console.log($(this).find("title").text())
	            $("#container-"+parseInt(id)).append(
	                '<div class="painting"><img src="' + $($(xml).find("image")[parseInt(id)]).text() + '" width="200" height="225" alt="' + $($(xml).find("title")[parseInt(id)]).text() + '" /> <br/> <div class="title">' + $($(xml).find("title")[parseInt(id)]).text() + '<br/>$' + $($(xml).find("price")[parseInt(id)]).text() + '</div></div>');

	    }
	    $('#myModal, #myModal1').on('hidden.bs.modal', function () {
			$('.text-container').empty();
		})
<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.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="modal-click btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" id="0">Open Modal 1</button>

  <button type="button" class="modal-click btn btn-info btn-lg" data-toggle="modal" data-target="#myModal1" id="1">Open Modal 2</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" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header 1</h4>
        </div>
        <div class="modal-body">
          <div class="container text-container" id="container-0"></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
  <div class="modal fade" id="myModal1" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header 2</h4>
        </div>
        <div class="modal-body">
          <div class="container text-container" id="container-1"></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
  
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.