0

I'm trying to get the value of an input, but I can't have this:

$('#inputID').val();

Inside a seperate variable.

I have the following code:

var click = "$('#highlight"+n+"').replaceWith('<span id="+id+ " style="+c+">" + $("#input" + n + "").val() + "</span>');$('#black"+n+"').hide();$('#replace_box"+n+"').remove();";

This variable is not receiving the value of the input.

Can you explain to me why the following will not work?

$('#highlight"+n+"').replaceWith('<span id="+id+ " style="+c+">" + $("#input" + n + `"").val() + "</span>');`

And can I please get a solution. Cheers.

9
  • 3
    Your ' and " don't match at all - are you sure that is the code you are actually using? Commented Oct 23, 2011 at 18:02
  • 2
    Don't put jQuery code as literal string - it's wrong and totally pointless. Commented Oct 23, 2011 at 18:04
  • 2
    If you have code in strings you are doing something wrong. Commented Oct 23, 2011 at 18:08
  • 1
    Yes Its correct code. Also this is my Unique Find/Replace System. You can find words on the document and then your suppose to be able to click each single word and then a box will open to replace just that one word. That's what I've been trying to do for weeks but I just can't find a solution for this! This has been so frustrating. And I was really hoping someone can finally give me the correct solution but everyone keeps on giving me useless comments(No Offence). But thanks for thanks for the help. You can see the Find/Replace system here: yolatools.yolasite.com/find-replace.php Commented Oct 23, 2011 at 18:09
  • 1
    The comments aren't "useless" and you should pay attention to them. Commented Oct 23, 2011 at 18:13

1 Answer 1

3

I try some mind reading but this code won't work because you have wrong quotes:

$("#highlight"+n).replaceWith("<span id="+id+" style="+c+">"+$("#input" + n).val() +"</span>");

in any case this code would be better:

var span = $('<span />', {id: id, style: c});
span.html($("#input" + n).val());
$("#highlight"+n).replaceWith(span);
Sign up to request clarification or add additional context in comments.

2 Comments

What does that second code do. Note im injecting that highlight code into an onclick using an var.
@Shawn31313 using a variable not "using an var" ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.