...

Hi! I’m Starx

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

Stop reloading on post a form with JQuery

Question by Raymond van Os

I have the following problem. I got two forms on my web page. On the first form can i select a users whit a select box. When i have selected the users in the form, i will send it to the another form on the same web page. How can i be sure that the first page does not reload when i post the form. It is not intended that when I enter some data and a user wants to add the data that i entered are gone by reloading the page by post. I know this can with jquery can but i have no idea how this goes.

Thanks

Answer by NAVEED

You can achieve this by using jQuery.ajax() function.

jQuery('#firstForm').live('submit',function(event) {
    $.ajax({
        url: 'GetUserInfo.php', // script to return user info in json format
        type: 'POST',
        dataType: 'json',
        data: $('#firstForm').serialize(),
        success: function( data ) {
            for(var id in data) {
                jQuery('secondForm#' + id).val(data[id]); // loop json data to populate each field of second form
            }
        }
    });
    return false;
});

GetUserInfo.php

  • Get user’s name from $_POST variable
  • Get User information from database.
  • Create an array of user info such that index of each value should represent id of a second form field.

For Example:

$userInfo = array( 'FirstName' => 'abc', 'LastName' => 'xyz', 'DOB' => '01/01/2000');

Here FirstName, LastName and DOB are the IDs of form elements in second form

  • echo you data into json format:

echo json_encode( $userInfo );

Answer by Starx

You dont need jQuery for this. You can give javascript:void(0); to simply stop the proceeding of a form like

<form action="javascript:void(0)" >

But, in case you want jQuery Solution

$("form").on("submit", function(e) {
// ^ Select the form with tag, or #idname or .classname
   // do stuff
   return false; //stop the submition or in your case, stop the reload

   /*
   return false also is equivalent to
        e.preventDefault(); 
        e.stopPropagation();
   So, you can scale your concept too :)
   */
});

However, what you are trying this with probably involves a hidden element in another form. So, I will give a small logical part of how the whole thing works.

Example HTML:

<form name="form1" id="form1">
    <select id="formselect1">
       ....
    </select>
</form>
<!-- And Form 2 -->

<form name="form2" id="form2">
    <input type="hidden" id="inputfromform1" name="inputfromform1" />
</form>

Example JS:

$("#form1").submit(function() {
    var tempval = $("#formselect1").val(); //get the value of form 1
    $("#inputfromform1").val(tempval); // and place in the element of form 2
    return false; //Here stop it
});
Read more

jquery fadeOut callback functions not working

Question by daniel.tosaba

I found it in Jquery documentation that fadeOut() accepts callback function but in my case that function is not executing?? Take a look:

$("div.icon").stop(0,0).fadeOut(250,function(){
    $("div.expanded").stop(0,0).fadeIn(250);    
});

$("div.expanded").stop(0,0).fadeOut(250,function(){
    $("div.icon").stop(0,0).fadeIn(250);
});

Do you guys see something that I am missing here??

Many thanks

Answer by Starx

Omit the stop(), you dont need that. It only stop the animation prematurely.

Read more

how to create a multiple border on a img?

Question by Rohit Azad

images link http://imageshack.us/photo/my-images/31/multiplebg.jpg/

Hello fri i want to used one image. I want to multiple border in single image is possible throw css

Answer by Kyle Sevenoaks

At the moment it’s not possible to make more than one border in CSS. You can kind of fake having three borders by using some clever css3 shadowing techniques.

div
{
    margin: 10px;
    border: 2px solid #f00;
    box-shadow:inset 0px 0px 2px 2px #0f0, 0px 0px 2px 5px #00f;
}

But I’d just go with adding the borders to the image as this isn’t supported by IE. (Maybe IE9 supports it.)

Answer by Starx

Nest your <img> inside multiple container and apply border to each

<div id="b1">

    <div id="b2">

        <div id="b3">
            <img src="" />
        </div> 

    </div> 

</div> 

Then apply border to each like

#b1 { border: 1px #000 solid; }
#b2 { border: 1px #f00 solid; }
#b3 { border: 1px #0f0 solid; }
img { border: 1px #00f solid; }

Or

Use a image that has multiple border on it and using border-image property

Read more

How to move an element styled by CSS stylesheet?

Question by shortspider

I have an image that I want to move. I can move the element with javascript if I have the HTML below:

<html>
<head>
    <title>Snake</title>
    <script type="text/javascript" src="snake.js"></script>
    <!-- <link type="text/css" href="snake.css"> -->
</head>
<body onKeyPress="ProcessKeypress(event);">
    <p><img id="snake" style="z-index: 0; left: 300px; position: absolute; top: 250px" 
    float=top border=0 hspace=0 src="snake.gif"></p>
</body>

However I want to stye the image element using CSS. When I do this my code to move the image does not work. HTML and CSS below are what I would like to use.

<html>
<head>
    <title>Snake</title>
    <script type="text/javascript" src="snake.js"></script>
    <link type="text/css" href="snake.css">
</head>
<body onKeyPress="ProcessKeypress(event);">
    <p><img id="snake" src="snake.gif"></p>
</body>

@CHARSET "UTF-8";
img {
z-index: 0;
left: 300px; 
position: absolute; 
top: 250px;
float: top;
border: 0;
hspace: 0;
}

JavaScript below is what I am using to move the image. Any and all help appreciated.

function moveObj(name, Xpix, Ypix) 
{    
    obj = document.getElementById(name);

    px = parseInt(obj.style.left) + Xpix;       
    py = parseInt(obj.style.top) + Ypix;
    obj.style.left = px;
    obj.style.top = py;
}

function ProcessKeypress(e)
{
    var myObj = 'snake';
    var moveBy = 10;

    if(e.keyCode === 97) {
        moveObj(myObj, -moveBy, 0);
    }
    else if(e.keyCode === 100) {
        moveObj(myObj, moveBy, 0);
    }
    else if(e.keyCode === 115) {
        moveObj(myObj, 0, moveBy);
    }
    else if(e.keyCode === 119) {
        moveObj(myObj, 0, -moveBy);
    }
}

Answer by Starx

obj.style.left accesses the style properties defined in inline only.

UPDATE

Here is your pure JavaScript Solution

Function: getStyleProp() to get the current applied style [Source]

function getStyleProp(elem, prop){
    if(window.getComputedStyle)
        return window.getComputedStyle(elem, null).getPropertyValue(prop);
    else if(elem.currentStyle) return elem.currentStyle[prop]; //IE
}

Function: moveObj()

function moveObj(obj, Xpix, Ypix) {  
    obj.style.left = parseInt(getStyleProp(obj,'left')) + Xpix+"px";
    obj.style.top= parseInt(getStyleProp(obj,'top')) + Ypix + "px";
}

Function: ProcessKeypress()

function ProcessKeypress(e) {
    var myObj = document.getElementById("snake");
    var moveBy = 10;
    switch(e.charCode) {
        case 97: moveObj(myObj, -moveBy, 0); break;
        case 100: moveObj(myObj, moveBy, 0); break;
        case 115: moveObj(myObj, 0, moveBy); break;
        case 119: moveObj(myObj, 0, -moveBy); break;            
    }
}    

I also solved your case using jQuery: Demo

Function moveObj

function moveObj(obj, Xpix, Ypix) {    
    obj.css('left', parseInt(obj.css('left')) + Xpix);       
    obj.css('top', parseInt(obj.css('top')) + Ypix);
}

Function ProcessKeypress

function ProcessKeypress(e) {
    var myObj = $("#snake"); //It will be better if it is cached
    var moveBy = 10;
    switch(e.charCode) { //used Switch for better manageability
        case 97: moveObj(myObj, -moveBy, 0); break;
        case 100: moveObj(myObj, moveBy, 0); break;
        case 115: moveObj(myObj, 0, moveBy); break;
        case 119: moveObj(myObj, 0, -moveBy); break;            
    }
}

Event Trigger attached to document instead

$(document).on('keypress', ProcessKeypress);
Read more

Is there a way to test for a scrollbar with just CSS?

Question by Jared

I want to know if the element is showing vertical scrollbars or not, and if it is possible to do this with CSS only.

This only needs to work for Firefox by the way.

Answer by BoltClock

If you mean using selectors to test, no, there isn’t such a selector in standard CSS (because the presence of scrollbars is calculated during rendering), nor can I find any selectors in this list of Mozilla vendor extensions that do what you’re looking for.

Answer by Starx

No, CSS cannot accomplish as this requires to be able to monitor the element, not apply styles.

Using jQuery

   var element = $("#yourdiv");
   if(element.get(0).scrollHeight > element.height()) {
       console.log('scroll bar is visible');
   }
Read more

How to make recessed button in CSS

Question by CodeDevelopr

In the image below, the top image is what I currently have on a site, it uses an actual Image.

What I want to do is make something more like the bottom image using pure CSS, I cannot figure out how to do it though, can anyone help?

enter image description here

Answer by theJollySin

CSS3 has a lot of solutions. Try this:

div.exampleboxshadowj {
background-color: #EEE;
float: left;
margin-top: 20px;
margin-right: 40px;
height: 65px;
width: 160px;
text-align: center;
-webkit-box-shadow: inset -5px -5px 5px 5px#888;
box-shadow: inset -5px -5px 5px 5px #888;
}

Though you can change the color to blue, if you want.

Here is a great link for all the info you could want.

Answer by Starx

If I understood you properly, you are trying to change the image when it is clicked or is active. If so, for that you can use :focus or :active to generate such effect.

Here is a demo with background color instead of a picture.

Read more

CSS to match the last match of a selector?

Question by Walkerneo

I have a structure like:

<div id="holder">
    <div id="first"></div>
    <div class="box">I'm the first</div>
    <div class="box">Nothing special</div>
    <div class="box">Nothing special</div>
    <div class="box">Nothing special</div>
    <div class="box">Nothing special</div>
    <div class="box">Nothing special</div>
    <div class="box">Make this background orange!!!</div>
    <div id="last"></div>
</div>​

I need the last .box element to have an orange background.

.box:last-child{} won’t work because it isn’t the last child. Apart from wrapping only the .box‘s in an element, is there any way to do this? This problem does represent what I’m trying to do, but I’d also like to know if there is a way to match the last matched element of a selector.

http://jsfiddle.net/walkerneo/KUa8B/1/

Extra notes:

  • No javascript
  • No giving the last element an extra class

Answer by BoltClock

Sorry, aside from abusing sibling combinators or :nth-last-child() in a way that depends fully on your HTML structure1, it’s currently not possible with CSS selectors alone.

This very feature looks slated to be added to Selectors 4 as :nth-last-match(), though, which in your case would be used like this:

:nth-last-match(1 of .box) {
    background: orange;
}

But I don’t know if the syntax is going to change or when vendors will start implementing it, so let’s just leave that as a hypothetical-theoretical-maybe kind of thing for now.


1 Something like this, given that your last .box is also the second last child:

.box:nth-last-child(2) {
    background: orange;
}

Answer by Starx

You can give nth-last-of-type() for a workaround

#holder > .box:nth-last-of-type(2) {
    background:orange;
}

Demo

However, here is also another way of getting this done

<div id="holder">
    <div id="first"></div>
    <div class="boxes">
        <div class="box">I'm the first</div>
        <div class="box">Nothing special</div>
        <div class="box">Nothing special</div>
        <div class="box">Nothing special</div>
        <div class="box">Nothing special</div>
        <div class="box">Nothing special</div>
        <div class="box">Make this background orange!!!</div>
    </div>
    <div id="last"></div>
</div>

Then, you can use

.boxes .box:last-child {
    background:orange;
}

Demo

Read more

Changing elements CSS properties based on child element

Question by James Dawson

Possible Duplicate:
Complex CSS selector for parent of active child

This is my markup:

<ol class="wp-paginate">
    <li><span class="title"></span></li>
    <li><span class="page current">1</span></li>
    <li><a href="http://localhost/page/2/" title="2" class="page">2</a></li>
    <li><a href="http://localhost/page/2/" class="next">»</a></li>
</ol>

And I want to change the background-position property of the li element that has a span child with the class current. In this case, the second list item.

I do consider myself proficient with CSS, but I really cannot think of how to do that. Maybe there isn’t a way, or I’m just having a brain fart.

Thanks!

Answer by Starx

Its nearly impossible with javascript.

Using jQuery

$("li").each(function() {
  if($(this).find("span.current").length > 0) {
    $(this).css("backgroundPostion", "top left");  
  }
});
Read more
March 26, 2012

How to make same layout for all web pages

Question by mainajaved

I am currently working on HTML I want to ask a question about website development.I am developing a website in which the basic layout remains same like menu, side menu etc but only the content changes.Currently I have make separate .html file for all web pages.
Can any one tell me is there a way through which I can make a separate file having etc common to all and call it in my html file.I have heard about CSS but it will only change the style and layout.
thanks

Answer by Starx

This is very big topic to include in just one answer. SO I will give only the logical part.

Separate your template into multiple chunks like:

1. header.php
2. leftSidebar.php
4. rightsidebar.php
5. footer.php

Now, include these common part on your every page.

For example: index.php

<?php

    include "header.php";
    include "leftSidebar.php";
    echo "<div>".$thedifferentpart."</div>"; //Change only this part on every other page you will create.
    include "footer.php";

?>

NOTE: This is only a logical part, applying the concept on your code

Read more

adding previous array values to newer array values

Question by steve

$test = 'test1/test2/test3/test4';

I am trying to get a array from that $test above which i’d like to output like below.

Array
(
    [0] => /
    [1] => /test1/
    [2] => /test1/test2/
    [3] => /test1/test2/test3/
    [4] => /test1/test2/test3/test4/
)

I’ve tried loops but can’t quite figure out how to get it quite right.

Answer by Starx

A very easy and simple way, for this case would be

$test = 'test1/test2/test3/test4';
$arr = explode("/", $test);

$t = "";
$newArray = array("/");
foreach($arr as $value) {
   $t .= "/".$value;
   $newArray[] = $t;
}

print_r($newArray);

Demo

Read more
...

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