Tayyab
Tayyab

Reputation: 184

Jquery Selecting Right After appending

There is a button in my page and after clicking on it, it append some element with class .select2-selection__rendered. So i want to get its text after appending and it don't appends when the page loads, it appends when i click on a button and after that i want to use it in AJAX call but when i was testing it by alerting it's text i got nothing, its blank! . There is not a problem with appending. It appends it's span element. Here is my script of selecting it's text.

$(document).ready(function(){  
          function () {
        var postTitle = $(".select2-selection__rendered").text(); //selecting text
        $("div#sdf").click(function(){ //this div is like a button
              alert("Title is: " + postTitle);
          });
      });  

==============================NEW EDITED===========================

Hey guys, i don't want to append something, i want to select the text of element that is being appended by script and that script only run when i click myButtn(name as example). And that script is not written by me and it's too long, i downloaded it...wait let me show you an example

See the Above image....Now see this below image

See the Above image....Now see this below image

enter image description here

Now any suggestions or help....? :(

==============================EDITED=================================

Upvotes: 1

Views: 103

Answers (2)

OxyDesign
OxyDesign

Reputation: 754

If I understood right, you mean something like this :

$(document).ready(function(){  
  $("div#sdf").click(function(){
    var select2 = $(".select2-selection__rendered").appendTo('body'); // replace 'body' by the DOM element to append to
    var postTitle = select2.text();
    alert("Title is: " + postTitle);
  });
});

or even

$(document).ready(function(){  
  $("div#sdf").click(function(){
    var postTitle = $(".select2-selection__rendered").appendTo('body').text(); // replace 'body' by the DOM element to append to
    alert("Title is: " + postTitle);
  });
});

based on edited comment I think this should work :

$(document).ready(function(){  
  $("div#sdf").click(function(){
    var postTitle = $(".select2-selection__rendered").text();
    alert("Title is: " + postTitle);
  });
});

Upvotes: 1

Jobelle
Jobelle

Reputation: 2834

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

<script>

    $(document).ready(function(){  

            var postTitle = $(".select2-selection__rendered").text(); //selecting text
            $("div#sdf").click(function () { //this div is like a button
                $(".select2-selection__rendered").append("<span style='color:red;'>hello</span>")
            });
        });  
</script>
</head>
<body>
   <div class="select2-selection__rendered" >
        sample Text
    </div>
    <br />
    <div id="sdf">Click Me</div>
</body>
</html>

Upvotes: 0

Related Questions