最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to interchange two <div> position in HTML using jquery on selection of any dropdown value? - Stac

programmeradmin2浏览0评论

I* have two <div> containers which needs to interchange their position of display on the change of a dropdown value.

So, how can this be achieved using Jquery?

bewlow are the two div containers who display position is needed to be interchanged

<div id="first"><p>I will be displayed on second place on dropdown's particular value</p></div>

<div id="second"><p>I will be displayed on first place on dropdown's same value for which first div is placed at second position</p></div>

I* have two <div> containers which needs to interchange their position of display on the change of a dropdown value.

So, how can this be achieved using Jquery?

bewlow are the two div containers who display position is needed to be interchanged

<div id="first"><p>I will be displayed on second place on dropdown's particular value</p></div>

<div id="second"><p>I will be displayed on first place on dropdown's same value for which first div is placed at second position</p></div>
Share Improve this question asked Feb 27, 2013 at 6:42 OM The EternityOM The Eternity 16.2k44 gold badges125 silver badges187 bronze badges 1
  • Possible duplicate: stackoverflow./q/9030322/875127 – Cianan Sims Commented Feb 27, 2013 at 6:45
Add a ment  | 

2 Answers 2

Reset to default 3

Simple Using Javascript

<script>

function interchange()
{
 var strDiv1Content = document.getElementById('first').innerHTML;
 var strDiv2Content = document.getElementById('second').innerHTML;

 document.getElementById('first').innerHTML = strDiv2Content;
 document.getElementById('second').innerHTML = strDiv1Content;

}

</script>

using jQuery

<script>
function interchange()
{
  var strDiv1Cont = $("#first").html();
  var strDiv2Cont = $("#second").html();

  $("#first").html(strDiv2Cont);
  $("#second").html(strDiv1Cont);
}

</script>

Call function interchange on some button click like

<input type ='button' value ='interchange' onclick='interchange' />

This will do it.

var firstHtml = $('#first').html();
var secondHtml = $('#second').html();

$('#second').html(firstHtml);
$('#first').html(secondHtml);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论