1

I have a div in which I have to fill some data in. I have to render the HTML based on conditions and I am adding data to that div using jQuery. Can someone please tell me how I can add the condition based insertion of HTML on the page?

      function AddData()
    {   
        var data = "<div><h1>My data</h1>"
             if(jsVariable){
                  <p>The JSVariable is present on page </p>                
                        }
               +"</div>"
            $('.myDiv').after("<br/>"+data);
    }
2
  • What exactly are you trying to do? If you simply want to add some extra html content depending on the variable, you are almost done. You just need to add the <p> part to the data (data += "<p>...</p>"). Commented Mar 30, 2014 at 22:13
  • I have a lot of HTML to add based on certain conditions like if the data is present or not .I used if(jsvariable) example to simplify my case Commented Mar 30, 2014 at 22:18

8 Answers 8

2
  function AddData()
    {   
        var data = "<div><h1>My data</h1>"
             if(jsVariable){
                  data = data + "<p>The JSVariable is present on page </p>"     
              }
             data = data + "</div>"
            $('.myDiv').append("<br/>"+data);
    }
Sign up to request clarification or add additional context in comments.

Comments

1
  function AddData(){   
    if(typeOf(jsVariable)!=="undefined"){ 
      var data =  "<div><h1>My data</h1>";
          data += "  <p>The JSVariable is present on page </p>";                
          data += "</div>";
       $('.myDiv').after("<br/>"+data);     
      }
}

this should do the trick, but some element with a class of myDiv will need to already exist for this to work

 <div class="myDiv"></div>

1 Comment

Thanks all..Really appreciate so quick responses.
0

What exactly are you trying to do?

If you simply want to add some extra html content depending on the variable, you are almost done. You just need to add the <p> part to the data (data += "<p>...</p>").

If you're trying to add all of the html based on the variable, put the if statement around the $(".myDiv").after (which should be $(".myDiv").append btw).

Comments

0

You'r code is not valid.

Could you explain what do you want to do with

if(jsVariable){
    <p>The JSVariable is present on page </p>                
}
+"</div>"

If you want to add a html code at ending of a div, you should use

$('.myDiv').append('<p>Text text text text</p>');

Comments

0

simple usage:

<!DOCTYPE html>
<html>
<head>
<script
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">

</script>
<script>
    $(document).ready(function() {
        $("p").click(function() {
            var a = $("#div1").text();
            if (a == "one") {
                $("#div2").text(a);
            }
        });
    });
</script>
</head>
<body>
    <div id="div1">one</div>
    <div id="div2"></div>
    <p>click me</p>
</body>
</html>

Comments

0
function addData(to)
{
    var h1   = $('<h1>').text('My Data');
    var data = $('<div>').append(h1);

    if (window.jsVariable) {
        $('<p>').text('JS Variable is present on the page').appendTo(data);
    }

    to.append(data);
}

addData( $('.myDiv') );

Comments

0
if (condition) {
   $('div#yourDivID').html('<p>The JSVariable is present on page </p>');
}

In addition to .html(which places the html inside your div), you can use other things like append, prepend etc. Check out jQuery's documentation on DOM Insertion.

Comments

0

Here is a JSFiddle http://jsfiddle.net/va4n8/

function addData() {
    var newDiv = $('<div>');
    newDiv.append($('<h1>').html('My data'));

    if ('jsVariable' in window) {
        newDiv.append($('<p>').html('The JSVariable is present on page'));
    }

    $('.mydiv').after($('<br>')).after(newDiv);
}

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.