2012-11-16 2 views
3

나는이 HTML 마크 업이 있습니다jQuery를, 앞에 추가 데이터는

<div id="destination"> 

</div> 

<input id="test" type="text" /> 

<button id="clicker">Click me</button>​ 

과 경고 텍스트가 필드에 inputed 텍스트가 참으로

$(document).on('click', '#clicker', function(event){ 

    var newCat = $('#test').val(); 
    $(newCat).prepend("#destination"); 
     alert(newCat); 
});​ 

이 jQuery를-조각을하지만, 텍스트가 #destination div에 있지 않습니다. http://jsfiddle.net/FaAY4/

+0

가 왜 이벤트가'document'에 위임된다? –

답변

3

당신은이 작업을 수행해야합니다 :

$(document).on('click', '#clicker', function(event){ 

    var newCat = $('#test').val(); 
    $("#destination").prepend(newCat); 
     alert(newCat); 
     }); 
+0

감사! 내 원본 파일 (jsfiddle을 만들기 전에)에서 prependTo를 사용했습니다. 어쨌든, 당신의 대답은 작동 :) –

1

왜 값에 elemet을 추가하려고

이 jsfiddle 예제를 참조하십시오?

var newCat = $('#test').val(); 

위의 라인은 당신에게 텍스트 상자의 값을 제공 할 것입니다.

$(newCat) 

일부 요소와 일치하지 않을 수도 있습니다.

1

나는 당신이 소스 및 대상 즉 스와핑 생각 : 당신은 잘못 prepend()을 사용하고

$(document).on('click', '#clicker', function(event){ 
    var newCat = $('#test').val(); 
    $('#destination').prepend(newCat); 
     alert(newCat); 
});​ 

Fiddle updated

2

. 구문은 다음과 같습니다

$(targetElement).prepend(newContent); 

사용자의 특정 예에서 다음과 같이 사용합니다 :

$("#destination").prepend(newCat);