...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
April 24, 2012

put div and inside <td> tag

Question by vikas tyagi

it’s dynamic code and i have design issue with that

<td id="test" class="test1"></td>
<div id="WidgetButton" class="right">test</div> // that part of code making dyamic


  there is any way put that <div> has id WidgetButton and inside <td></td>

like that

<td id="test" class="test1">
<div id="WidgetButton" class="right">test</div>
</td>

plz suggest me how can i do it with jquery

thanks

Answer by T.J. Crowder

As both of them have ids, this is a trivial application of the jQuery function (usually aliased as $) and appendTo.

$("#WidgetButton").appendTo("#test");

It’s just as well they both have ids, too, because otherwise it would be tricky, since the markup is invalid and so browsers will relocate the div to where they guess it should go, so using structural selectors and relative traversal would be difficult. The id values trump that, thankfully.

Answer by Starx

Update:

Since you only want to do this for div with id WidgetButton. Heres how

 $("#WidgetButton").appendTo("#test");

If you to remove all divs and put inside a td, it might prove to be dangerous. But you can do this like

$("div").each(function() {
    $(this).appendTo("#test");
});

However, I will extremely recommend you narrow your selection to a particular group of divs like using class names or ids.

ClassName

$(".divswithclassname").each(function() {
    $(this).appendTo("#test");
});

Certain Id

$("#idofdiv")..appendTo("#test");
Read more

JavaScript not resizing height of UL element sometimes when inserting LI elements using Jquery

Question by Fire Emblem

I have an Html/JavaScript application that contains N columns that need to be large enough contain all of the possible LI elements from all of the columns.

The simple solution seems to count the heights of all of the items in each column, compensate for padding, and then set the height to that total for each of the columns.

This works great when the LI elements contain plain text. Unfortunately, when the LI elements contain images instead, various browsers have problems. For example, when I first load the page in FireFox, it looks like the screenshot below, but upon another refresh, it works fine. It doesn’t work as expected in Chrome either.

Screenshot Showing the height of the UL element is not in sync with the LI elements

My application does not pre-populate the LI elements when the page loads – it uses JavaScript, as follows:

function populateUnsetAnswers(unsetCategoryAnswers) {
    for (i in unsetCategoryAnswers) {
        if (unsetCategoryAnswers.hasOwnProperty(i.toString())) {
            $('#categoryQuestionArea #possibleAnswers').append(
                categoryAnswerLiTag(unsetCategoryAnswers[i])
            );
        }
    }
}

function categoryAnswerLiTag(unsetCategoryAnswer) {
    var html = '<li id="' + unsetCategoryAnswer.id + '">';

    if (unsetCategoryAnswer.image) {
        html += '<img class="categoryAnswerImage" title="';
        html += unsetCategoryAnswer.text;
        html += '" src="/trainingdividend/rest/streaming/';
        html += unsetCategoryAnswer.image.fileName;
        html += '" style="height: ';
        html += unsetCategoryAnswer.image.height;
        html += ';';
        html += '" />';
    } else {
        html += unsetCategoryAnswer.text
    }

    html += '</li>';

    return html;
}

When the page is done loading, an ajax request fetches all of the objects to be put into LI elements, and then calls the first function above.

After all of the LI elements are created, I call this function right after it:

function resize() {
    var currentHeight, totalHeight;
    totalHeight = 0;

    $("#categoryQuestionArea ul").children().each(function() {
        currentHeight = $(this).height();

        totalHeight += currentHeight + 13;
    });

    $("#categoryQuestionArea ul").height(totalHeight);
    $("#categoryQuestionArea div#separator").css("padding-top", (totalHeight / 2) + "px");
}

Is there any way to tell jQuery, “Don’t call resize() until all of the LI’s are fully loaded and the images have rendered” ?

I think what’s happening is that on the initial page load, the height of these LI elements is 0 or a small value because it doesn’t contain the image, so my resize function is calculating the wrong result (I tested this with some alert statements). As long as the LIs are populated and the images have loaded, the total height is calculated just fine.

Any help? Thanks

Answer by Starx

This is rather a CSS problem, most probably due a fixed height, with items either floated or absolutely positioned.

There are number of ways to fix this.

  1. Give a min-height instead of fixing a height.

    #container { min-height: 100px; }
    
  2. Clear the float and do not set any heights

    #container { overflow: hidden; }
    
  3. Use Scripts to add up to the height, once every element is added. Like the jQuery snippet below

    $("#container").append($("#theimg"));
    $("#container").height($("#container").height()+$("#theimg").height());
    
Read more

What's wrong? I am receiving error msg: Invalid arguments passed in eval()

Question by user6919

I write down my php code:

<?php
        // 已有指定 material, 顯示 material 資訊
        if (strlen($m_id) > 0) {
            // 此 material 屬於哪些 mgroup
            $group_info = $mUtil->groupInfo($m_id);
            $group_names = array();
            foreach ($group_info as $mg_id => $row) {
                if (!$row["not_in_group"]) {
                    $group_names[] = $row["mg_name"];
                }
            }
        }
    ?>

  <table width="100%">
    <tr>
      <th colspan="2"><?php echo $m_name; ?></th>
    </tr>
    <tr class="odd">
      <th>Formula</th>
      <td width="80%"><?php echo $formula; ?></td>
    </tr>
    <tr class="odd">
      <th>Alias</th>
      <td><?php echo $alias; ?></td>
    </tr>
    <tr class="odd">
      <th>In groups</th>
      <!-- join() == implode() -->
      <td><?php echo join($group_names, ",&nbsp; "); ?></td>
    </tr>
  </table><br /><br />

but I get these error message:

Notice: Undefined variable: group_names in eval() (line 97 of D:xampphtdocsdrupalmodulesphpphp.module(80) : eval()'d code).
Warning: join() [function.join]: Invalid arguments passed in eval() (line 97 of D:xampphtdocsdrupalmodulesphpphp.module(80) : eval()'d code).
Notice: Undefined variable: group_names in eval() (line 97 of D:xampphtdocsdrupalmodulesphpphp.module(80) : eval()'d code).
Warning: join() [function.join]: Invalid arguments passed in eval() (line 97 of D:xampphtdocsdrupalmodulesphpphp.module(80) : eval()'d code).

Anyone can help me? Thanks a lot….

Answer by Starx

The undefined errors are due to variables like $group_names only being defined once

if (strlen($m_id) > 0) { ... } //condition is true.

Make sure the variable you are using are instantiated before using them.

Using isset($instancename) can be one of the ways to deal with this.

Example:

if(!isset($group_names)) $group_names = array();
// ^ if $group_names is not found then at least initialize it as an empty array
//   so that the rest of the script can go easy

Also, join() needs a glue to join the array, which is not in correct order.

<?php echo join(", ", $group_names); ?>

NOTE: The use of eval() has to be however be discouraged

Read more

Is there a valid reason about putting JS in the head of document?

Question by markzzz

I mean : I know the JS is cached only if it come from a .js file. Also, 90% of my functions must be rendered when the page (html) is loaded (rendered), so it is better put JS before closing the body tag. (this prevent also to use document .ready(); and the loading of the page itself will be more faster).

So, which is the advantage on putting JS in the <head></head>? Expect the “order” of the code, which I don’t mind so much to be honest…

Answer by Starx

The important thing about the elements in the <head> section are that they are loaded, before the <body> starts to load.

This is a very efficient feature, which are used a lot (IMO).

Loading of libraries, scripts, that have to run before the DOM has loaded have to done in <head> section.


I will give you a scenario

Imagine, You needed to calculate the 30% size of the screen’s total size and assign it to the element inside.

It would be foolish and wait for the element to load big, then run the script to load resize it again.

Read more

Jquery sortable, how to disable drag and drop

Question by Meg Lepett

<ul id="box">
<li id="1">1</li>
<li id="2">2</li>
</ul>



$("#box").sortable({ update: function() {
    var order = $("#box").sortable("serialize");
    alert(order);
  }
});                                         

I would like to control the order by pressing buttons, and disable the drag and drop.
Everything works great but i could not figure out how to disable the drag and drop but still remain this list sortable?

Edit –

Ok now i start to understand this. I do not need to use sortable at all.
How to get the same result in order variable without using sortable?

Answer by Starx

On that case, you don’t need sortable at all. Just use .append() and .prepend() to shift the position.

$("#link-left").click(function() {
   $("#placetoshift").append($("#thesortablediv"));
});
Read more

PHP – array_search() fails on === true, but not on !== false?

Question by DaBananaboat

When I want to check if something is in the array and get the key back, I use the array_search() function.

Why is it when I compare the function to be exactly equal to true (=== true) it returns false, and when I compare it to not be exactly equal to false (!== false) it returns true?

<?php
    if(array_search($value, $array) === true)
    {
        // Fails
    }

    if(array_search($value, $array) !== false)
    {
        // Succeeds
    }
?>

Thanks in advance.

Answer by Starx

array_search() does not return true.

If will only return false, if it can’t find anything, otherwise it will return the key of the matched element.

According to the manual

array_search — Searches the array for a given value and returns the corresponding key if successful
….

Returns the key for needle if it is found in the array, FALSE otherwise.

Read more

how to disable scrolling in HTML files in iPad web

Question by Jackson J

I am using following code for my webpage but i have removed script but it still page swaping is with finger for ipad how to remove this. I have removed all the scripts but again it is swaping.I have tried

<body scroll='no'>  

but does not get any response

HTML file code

<!DOCTYPE HTML>
<html manifest="draxxin.manifest">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="apple-touch-icon" href="icon.png" />
    <title>BRD</title>
    <link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.2.6.css"
    />
    <link rel="shortcut icon" type="image/ico" href="images/favicon.gif" />
    <link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen"
    />
    <link type="text/css" href="newstyle.css" rel="stylesheet" />
    <script src="js/jquery-1.7.2.min.js"></script>
    <script src="js/jquery-ui-1.8.18.custom.min.js"></script>
    <script language="javascript" type="text/javascript">
        function showHideDiv()
        {
            document.getElementById("one").style.visibility = "visible";
            document.getElementById("two").style.visibility = "visible";

            document.getElementById("three").style.visibility = "hidden";
            document.getElementById("four").style.visibility = "hidden";


        }
    </script>
    <script language="javascript" type="text/javascript">
        function showHideView()
        {

            document.getElementById("one").style.visibility = "hidden";
            document.getElementById("two").style.visibility = "hidden";
            document.getElementById("three").style.visibility = "visible";
            document.getElementById("four").style.visibility = "visible";



        }
    </script>
    <script type="text/javascript">
        $(document).ready(function ()
        {
            $(".linkbutton").click(function ()
            {
                /* <!--   var address= $(this).attr("src");-->*/
                var address = $(this).attr("data-light");


                $("#popup").fadeIn("slow");
                $("#lightbox").attr("src", address);
            });
            $("#close").click(function ()
            {
                $("#popup").fadeOut("fast");
            });
        });
    </script>
    <script type="text/javascript">
        $(document).ready(function ()
        {
            $(".linkbutton_one").click(function ()
            {
                /* <!--   var address= $(this).attr("src");-->*/
                var address = $(this).attr("data-light-one");


                $("#popup_one").fadeIn("slow");
                $("#lightbox_one").attr("src", address);
            });
            $("#close_one").click(function ()
            {
                $("#popup_one").fadeOut("fast");
            });
        });
    </script>
    <script type="text/javascript">
        $(document).ready(function ()
        {
            $(".linkbutton_two").click(function ()
            {

                /* <!--   var address= $(this).attr("src");-->*/



                var address = $(this).attr("data-light-two");


                $("#popup_two").fadeIn("slow");
                $("#lightbox_two").attr("src", address);
            });
            $("#close_two").click(function ()
            {
                $("#popup_two").fadeOut("fast");
            });
        });
    </script>
    <style type="text/css">
        #popup {
            background:#FFFFFF;
            height:75%;
            width: 100%;
            position:absolute;
            display: none;
        }
        #center {
            border: 10px solid #121212;
            margin: 6% 0 0 20%;
            width: 572px;
        }
        #close {
            float: right;
            position:absolute;
            margin:25px 0px 0 0;
            top:0px;
            left:510px;
        }
    </style>
    <style type="text/css">
        #popup_one {
            background:#FFFFFF;
            height:100%;
            width: 100%;
            position:absolute;
            display: none;
        }
        #center_one {
            border: 10px solid #121212;
            margin: 6% 0 0 20%;
            width: 318px;
        }
        #close_one {
            float: right;
            position:absolute;
            margin:25px 0px 0 0;
            top:0px;
            left:510px;
        }
    </style>
    <style type="text/css">
        #popup_two {
            background:#FFFFFF;
            height:100%;
            width: 100%;
            position:absolute;
            display: none;
        }
        #center_two {
            border: 10px solid #121212;
            margin: 6% 0 0 20%;
            width: 318px;
        }
        #close_two {
            float: right;
            position:absolute;
            position:relative;
            margin:25px 0px 0 0;
            top:0px;
            left:510px;
        }
    </style>
</head>

<BODY onLoad="showHideDiv()">

    <body>
        <div id="container">
            <div id="page_one" class="panel">
                <div class="main_heading">Overview</div>
                <div class="menu">
                    <ul>
                        <li class="stix"></li>
                        <li>
                            <a href="#page_one"><img src="image/overview_active.png"/></a>
                        </li>
                        <li class="stix"></li>
                        <li>
                            <a href="#page_two"><img src="image/strategy.gif"/></a>
                        </li>
                        <li class="stix"></li>
                        <li>
                            <a href="#"><img src="image/draxxin.gif"/></a>
                        </li>
                        <li class="stix"></li>
                        <li>
                            <a href="#"><img src="image/excede.gif"/></a>
                        </li>
                        <li class="stix"></li>
                        <li>
                            <a href="#"><img src="image/results.gif"/></a>
                        </li>
                        <li class="stix"></li>
                        <li>
                            <a href="#"><img src="image/reference.gif"/></a>
                        </li>
                        <li class="stix"></li>
                    </ul>
                </div>
                <div class="rightclass">
                    <img src="image/pageone_image.png" />
                </div>
                <div>
                    <ul>
                        <li class="midmenu">
                            <a href="index.html"><img src="image/backward.png"/></a>
                        </li>
                        <!--#page_one-->
                        <li class="midmenu1">
                            <a href="#page_two"><img src="image/forward.png"/></a>
                        </li>
                    </ul>
                </div>
                <div class="text_paragraph">
                    <p>Bovine Respiratory Disease (BRD) is the leading cause of economic loss
                        in the beef industry.
                        <small>1, 2</small>
                    </p>
                </div>
                <div class="text_paragraph_1">
                    <b>Economic loss:</b>
                    <ul>
                        <li>Costs the industry an estimated $1 billion each year.
                            <small>3, 4</small>
                        </li>
                        <li>Losses can be attributed to death, reduced feed and treatment costs.
                            <small>3,4</small>
                        </li>
                        <li>Economic losses range from an estimated $57.48 to $239.69 per head.
                            <small>1, 2</small>
                        </li>
                    </ul>
                </div>
                <div class="text_paragraph_2">
                    <b>Bacterial Culprits:</b>
                    <p>Four bacterial pathogens are commonly associated with BRD.</p>
                    <ul>
                        <li>Mannheimia haemolytica</li>
                        <li>Pasteurella multocida</li>
                        <li>Histophilus somni</li>
                        <li>Mycoplasma bovis</li>
                    </ul>
                </div>
                <div>
                    <img src="image/smallicon_2.png" class="linkbutton">
                    <img src="image/smallicon_2.png" class="linkbutton" data-light="image/popup_page_1.png"
                    />
                </div>
                <div id="popup">
                    <div id="center">
                        <img id="lightbox" src="image/popup_page_1.png">
                        <img id="close" src="images/close.png" alt="close">
                    </div>
                    <!-- #center -->
                </div>
            </div>
            <!-- End of page one-->
            <div id="page_two" class="panel">
                <div class="main_heading_two">Strategy</div>
                <div class="menu">
                    <ul>
                        <li class="stix"></li>
                        <li>
                            <a href="#page_one"><img src="image/overview.gif" /></a>
                        </li>
                        <li class="stix"></li>
                        <li>
                            <a href="#page_two"><img src="image/strategy_active.png"/></a>
                        </li>
                        <li class="stix"></li>
                        <li>
                            <a href="#"><img src="image/draxxin.gif"/></a>
                        </li>
                        <li class="stix"></li>
                        <li>
                            <a href="#"><img src="image/excede.gif"/></a>
                        </li>
                        <li class="stix"></li>
                        <li>
                            <a href="#"><img src="image/results.gif"/></a>
                        </li>
                        <li class="stix"></li>
                        <li>
                            <a href="#"><img src="image/reference.gif"/></a>
                        </li>
                        <li class="stix"></li>
                    </ul>
                </div>
                <div class="rightclass_one">
                    <img src="image/whiteslide.png" />
                </div>
                <div class="pagetwo_text">
                    <p>Get up to 21 days of BRD therapy strategy using longer-duration antimicrobials
                        DRAXXIN ® (tulathromycin) Injectable Solution and EXCEDE ® (ceftiofur crystalline
                        free acid) Sterile Suspension.</p>
                </div>
                <div class="pagetwo_list_text">Advantages for Producers:
                    <ul>
                        <li>Fewer treatments</li>
                        <li>Less stress on cattle</li>
                        <li>Fewer mortalities</li>
                        <li>Cattle can recover in their own pen</li>
                        <li>Lower treatment costs and higher profits</li>
                    </ul>
                </div>
                <div class="pagetwo_list_text2">Extended duration strategy</div>
                <div>
                    <ul>
                        <li class="midmenu_1">
                            <a href="#page_one"><img src="image/backward.png"/></a>
                        </li>
                        <li class="midmenu_2">
                            <a href="#page_two"><img src="image/forward.png"/></a>
                        </li>
                    </ul>
                </div>
                <div class="right_text">
                    <p>Either way you use them, it takes both DRAXXIN and EXCEDE for control
                        and treatment of BRD in high-risk cattle to give you the longest duration
                        of BRD therapy</p>
                </div>
                <div class="image_21">
                    <img src="image/pagetwo_graph_two_4.png" />
                </div>
                <div class="ratemeter">
                    <img src="image/pagetwo_graph_one.png" />
                </div>
                <div id="one" class="image_one">
                    <img src="image/optiononetwo.png" />
                </div>
                <div id="two" class="image_two">
                    <img src="image/optiononeone.png" />
                </div>
                <div id="three" class="image_one_one">
                    <img src="image/pagetwo_graph_two_11.png" />
                </div>
                <div id="four" class="image_two_two">
                    <img src="image/pagetwo_graph_two_22.png" />
                </div>
                <div class="option_image">
                    <img src="image/option_1.png" onclick="showHideDiv()" />
                </div>
                <div class="option_image_label">Option 1</div>
                <div class="option_image_one">
                    <img src="image/option_1.png" onclick="showHideView()" />
                </div>
                <div class="option_image_label_one">Option 2</div>
                <div class="fourteen_day">
                    <img src="image/button_14days.PNG" />
                </div>
                <div class="heading_one">Post Metaphylaxis Interval (PMI)</div>
                <div>
                    <img src="image/smallicon_1.png" class="linkbutton_one">
                    <img src="image/smallicon_1.png" class="linkbutton_one" data-light-one="image/popup_2_page_2.png"
                    />
                </div>
                <div id="popup_one">
                    <div id="center_one">
                        <img id="lightbox_one" src="image/popup_2_page_2.png">
                        <img id="close_one" src="images/close.png" alt="close">
                    </div>
                    <!-- #center -->
                </div>
                <div class="seven_day">
                    <img src="image/button_7days.PNG" />
                </div>
                <div class="heading_two">Post Treatment Interval (PTI)</div>
                <!-- <div class="linkbutton_two">
        <a href="image/popup_2_page_2.png" rel="lightbox"><img src="image/smallicon_1.png"  alt="" /></a>
       </div>
        -->
                <div>
                    <img src="image/smallicon_1.png" class="linkbutton_two">
                    <img src="image/smallicon_1.png" class="linkbutton_two" data-light-two="image/popup_2_page_2.png"
                    />
                </div>
                <div id="popup_two">
                    <div id="center_two">
                        <img id="lightbox_two" src="image/popup_2_page_2.png">
                        <img id="close_two" src="images/close.png" alt="close">
                    </div>
                    <!-- #center -->
                </div>
            </div>
            <!-- End of page two-->
        </div>
    </body>
</html>

Answer by Starx

Just hide the scrolling

<style>
    body, html { overflow: hidden; }
</style>
Read more

Remove duplicated values function

Question by user1345545

function remove_values($arr){
    $_a = array();
    while(list($key,$val) = each($arr)){
        $_a[$val] = 1;
    }
    return array_keys($_a);
}

i can’t follow the above function well.i don’t know what’s the effect of $_a[$val] = 1; anyone can explain it to me thank you

Answer by Robin Castlin

Well all this does is to insert the value into the key of an array. Since all keys are unique in an array, you will remove all duplicates.

array_keys() simply gives back that array in it’s ordinary form.

Example:

$arr = array('red', 'green', 'red');

$arr = remove_values($arr);

gives

array( 'red' => 1,
       'green' => 1);

which results in

array('red', 'green');

since “red” can only be a keyvalue once.

Answer by Starx

For the purpose of the function, Why not just use array_unique($array)?

Like this

function remove_values($arr){
    return array_keys(array_unique($arr));
}

Update:

Ok, Since you are trying to learn. Here is the explanation for the function in the post

function remove_values($arr){
    $_a = array(); //Creates a temporary array to store matching
    while(list($key,$val) = each($arr)){ //till their is item is array, loop it 
        $_a[$val] = 1; //Store in the temporary array with array's values as index, thus eliminating the duplicates
           //^ Here storing with the value as index IS THE KEY
    }
    return array_keys($_a); //return only the keys, which in fact are the unique values
}
Read more

Dynamic height increase of one div with respect another div

Question by Srinivas

I have two divs. These two divs are orientated as two vertical columns next to each other. Instead of pre-determining the height of the divs via css I want to have it grow dynamically with the content I put into it. Which is simple enough for one div but my problem is that I want the div on the left with background color green to grow to the same height of the div on the right . There is always going to be more content in the right than in left.

Answer by Starx

Assuming the elements are after body. Give 100% to the body, and all the div

body, #div1, #div2 { height: 100%; }

If they are not, then you have to either fix the height of the parent or chain 100% height all the way to the body again.

#parent { height: 800px; }
#div1,#div2 { height: 100%; }
Read more
...

Please fill the form - I will response as fast as I can!