How to replace form text string with div text
up vote
0
down vote
favorite
I have a form and I am using an onkeyup
event to search for matches within a database based on what the user types. That part is working just fine. Results trigger a hidden DIV to be shown with matches.
What I would then like to do is allow the user to click a matching result and using onclick
have that result inserted into the text field the user was typing in.
Here is what I have:
HTML
Distributor
<font color="red"><b>*</b></font>
<input type="text"
name="dist"
id="dist"
value="<? if (isset($dist)) {echo $dist;} ?>" required
onkeyup="sugdist(this.value);"/>
<br><br>
SCRIPT
function fillDist() {
var insertText = $(this).text();
$('#dist').insert(insertText);
}
Here is what I have on the AJAX script side.
while ($result = $query ->fetch_object()) {
echo "<div id='fillDist' OnClick='fillDist'>"
.addslashes($result->name).
"</div>";
}
It's not working, but its also not tripping any errors.
Any ideas?
Thanks
jquery html ajax forms
add a comment |
up vote
0
down vote
favorite
I have a form and I am using an onkeyup
event to search for matches within a database based on what the user types. That part is working just fine. Results trigger a hidden DIV to be shown with matches.
What I would then like to do is allow the user to click a matching result and using onclick
have that result inserted into the text field the user was typing in.
Here is what I have:
HTML
Distributor
<font color="red"><b>*</b></font>
<input type="text"
name="dist"
id="dist"
value="<? if (isset($dist)) {echo $dist;} ?>" required
onkeyup="sugdist(this.value);"/>
<br><br>
SCRIPT
function fillDist() {
var insertText = $(this).text();
$('#dist').insert(insertText);
}
Here is what I have on the AJAX script side.
while ($result = $query ->fetch_object()) {
echo "<div id='fillDist' OnClick='fillDist'>"
.addslashes($result->name).
"</div>";
}
It's not working, but its also not tripping any errors.
Any ideas?
Thanks
jquery html ajax forms
Where's the rest of the function that runs when the user clicks the result?
– APAD1
Nov 19 at 21:09
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a form and I am using an onkeyup
event to search for matches within a database based on what the user types. That part is working just fine. Results trigger a hidden DIV to be shown with matches.
What I would then like to do is allow the user to click a matching result and using onclick
have that result inserted into the text field the user was typing in.
Here is what I have:
HTML
Distributor
<font color="red"><b>*</b></font>
<input type="text"
name="dist"
id="dist"
value="<? if (isset($dist)) {echo $dist;} ?>" required
onkeyup="sugdist(this.value);"/>
<br><br>
SCRIPT
function fillDist() {
var insertText = $(this).text();
$('#dist').insert(insertText);
}
Here is what I have on the AJAX script side.
while ($result = $query ->fetch_object()) {
echo "<div id='fillDist' OnClick='fillDist'>"
.addslashes($result->name).
"</div>";
}
It's not working, but its also not tripping any errors.
Any ideas?
Thanks
jquery html ajax forms
I have a form and I am using an onkeyup
event to search for matches within a database based on what the user types. That part is working just fine. Results trigger a hidden DIV to be shown with matches.
What I would then like to do is allow the user to click a matching result and using onclick
have that result inserted into the text field the user was typing in.
Here is what I have:
HTML
Distributor
<font color="red"><b>*</b></font>
<input type="text"
name="dist"
id="dist"
value="<? if (isset($dist)) {echo $dist;} ?>" required
onkeyup="sugdist(this.value);"/>
<br><br>
SCRIPT
function fillDist() {
var insertText = $(this).text();
$('#dist').insert(insertText);
}
Here is what I have on the AJAX script side.
while ($result = $query ->fetch_object()) {
echo "<div id='fillDist' OnClick='fillDist'>"
.addslashes($result->name).
"</div>";
}
It's not working, but its also not tripping any errors.
Any ideas?
Thanks
jquery html ajax forms
jquery html ajax forms
edited Nov 19 at 21:07
Ali
351214
351214
asked Nov 19 at 19:43
Sabyre
228
228
Where's the rest of the function that runs when the user clicks the result?
– APAD1
Nov 19 at 21:09
add a comment |
Where's the rest of the function that runs when the user clicks the result?
– APAD1
Nov 19 at 21:09
Where's the rest of the function that runs when the user clicks the result?
– APAD1
Nov 19 at 21:09
Where's the rest of the function that runs when the user clicks the result?
– APAD1
Nov 19 at 21:09
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
The flow is not so clear.
So, the AJAX script returns an HTML that has an onClick
function.
I am assuming that the output is correctly added to your output div, because I see a simple error here:
OnClick='fillDist'
you should change it to
onClick='fillDist()'
to really call the function.
However, you have the jQuery tag and you are using it in some other operations, so you can avoid the inline onClick
in the HTML code, and do it all javascript/jQuery side:
$(document).on('click', '#fillDist', function() {
const text = $(this).text();
$('#dist').text(text);
});
We are doing this:
$(document).on('click', '#dist', function() {});
and not this:
$('#dist').click(function() {});
Because the element is dynamically injected.
$(document).on('click', '#fillDist', function() {
const text = $(this).text();
$('#dist').text(text);
});
#dist {
margin: 20px;
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dist">
</div>
<div id="fillDist">click me to add the same text to dist</div>
Please find the demo even here on codepen.
This works brilliantly for adding the text in a div. I cannot get it to insert the text into the form field.
– Sabyre
Nov 20 at 15:33
Was able to do it with:$('#dist').val(text);
– Sabyre
Nov 20 at 15:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
The flow is not so clear.
So, the AJAX script returns an HTML that has an onClick
function.
I am assuming that the output is correctly added to your output div, because I see a simple error here:
OnClick='fillDist'
you should change it to
onClick='fillDist()'
to really call the function.
However, you have the jQuery tag and you are using it in some other operations, so you can avoid the inline onClick
in the HTML code, and do it all javascript/jQuery side:
$(document).on('click', '#fillDist', function() {
const text = $(this).text();
$('#dist').text(text);
});
We are doing this:
$(document).on('click', '#dist', function() {});
and not this:
$('#dist').click(function() {});
Because the element is dynamically injected.
$(document).on('click', '#fillDist', function() {
const text = $(this).text();
$('#dist').text(text);
});
#dist {
margin: 20px;
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dist">
</div>
<div id="fillDist">click me to add the same text to dist</div>
Please find the demo even here on codepen.
This works brilliantly for adding the text in a div. I cannot get it to insert the text into the form field.
– Sabyre
Nov 20 at 15:33
Was able to do it with:$('#dist').val(text);
– Sabyre
Nov 20 at 15:41
add a comment |
up vote
2
down vote
accepted
The flow is not so clear.
So, the AJAX script returns an HTML that has an onClick
function.
I am assuming that the output is correctly added to your output div, because I see a simple error here:
OnClick='fillDist'
you should change it to
onClick='fillDist()'
to really call the function.
However, you have the jQuery tag and you are using it in some other operations, so you can avoid the inline onClick
in the HTML code, and do it all javascript/jQuery side:
$(document).on('click', '#fillDist', function() {
const text = $(this).text();
$('#dist').text(text);
});
We are doing this:
$(document).on('click', '#dist', function() {});
and not this:
$('#dist').click(function() {});
Because the element is dynamically injected.
$(document).on('click', '#fillDist', function() {
const text = $(this).text();
$('#dist').text(text);
});
#dist {
margin: 20px;
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dist">
</div>
<div id="fillDist">click me to add the same text to dist</div>
Please find the demo even here on codepen.
This works brilliantly for adding the text in a div. I cannot get it to insert the text into the form field.
– Sabyre
Nov 20 at 15:33
Was able to do it with:$('#dist').val(text);
– Sabyre
Nov 20 at 15:41
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The flow is not so clear.
So, the AJAX script returns an HTML that has an onClick
function.
I am assuming that the output is correctly added to your output div, because I see a simple error here:
OnClick='fillDist'
you should change it to
onClick='fillDist()'
to really call the function.
However, you have the jQuery tag and you are using it in some other operations, so you can avoid the inline onClick
in the HTML code, and do it all javascript/jQuery side:
$(document).on('click', '#fillDist', function() {
const text = $(this).text();
$('#dist').text(text);
});
We are doing this:
$(document).on('click', '#dist', function() {});
and not this:
$('#dist').click(function() {});
Because the element is dynamically injected.
$(document).on('click', '#fillDist', function() {
const text = $(this).text();
$('#dist').text(text);
});
#dist {
margin: 20px;
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dist">
</div>
<div id="fillDist">click me to add the same text to dist</div>
Please find the demo even here on codepen.
The flow is not so clear.
So, the AJAX script returns an HTML that has an onClick
function.
I am assuming that the output is correctly added to your output div, because I see a simple error here:
OnClick='fillDist'
you should change it to
onClick='fillDist()'
to really call the function.
However, you have the jQuery tag and you are using it in some other operations, so you can avoid the inline onClick
in the HTML code, and do it all javascript/jQuery side:
$(document).on('click', '#fillDist', function() {
const text = $(this).text();
$('#dist').text(text);
});
We are doing this:
$(document).on('click', '#dist', function() {});
and not this:
$('#dist').click(function() {});
Because the element is dynamically injected.
$(document).on('click', '#fillDist', function() {
const text = $(this).text();
$('#dist').text(text);
});
#dist {
margin: 20px;
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dist">
</div>
<div id="fillDist">click me to add the same text to dist</div>
Please find the demo even here on codepen.
$(document).on('click', '#fillDist', function() {
const text = $(this).text();
$('#dist').text(text);
});
#dist {
margin: 20px;
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dist">
</div>
<div id="fillDist">click me to add the same text to dist</div>
$(document).on('click', '#fillDist', function() {
const text = $(this).text();
$('#dist').text(text);
});
#dist {
margin: 20px;
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dist">
</div>
<div id="fillDist">click me to add the same text to dist</div>
answered Nov 19 at 22:15
Lorenzo S
1,111917
1,111917
This works brilliantly for adding the text in a div. I cannot get it to insert the text into the form field.
– Sabyre
Nov 20 at 15:33
Was able to do it with:$('#dist').val(text);
– Sabyre
Nov 20 at 15:41
add a comment |
This works brilliantly for adding the text in a div. I cannot get it to insert the text into the form field.
– Sabyre
Nov 20 at 15:33
Was able to do it with:$('#dist').val(text);
– Sabyre
Nov 20 at 15:41
This works brilliantly for adding the text in a div. I cannot get it to insert the text into the form field.
– Sabyre
Nov 20 at 15:33
This works brilliantly for adding the text in a div. I cannot get it to insert the text into the form field.
– Sabyre
Nov 20 at 15:33
Was able to do it with:
$('#dist').val(text);
– Sabyre
Nov 20 at 15:41
Was able to do it with:
$('#dist').val(text);
– Sabyre
Nov 20 at 15:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381580%2fhow-to-replace-form-text-string-with-div-text%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Where's the rest of the function that runs when the user clicks the result?
– APAD1
Nov 19 at 21:09