0

I would like to change value of an input field. I get the id value of the input tag through some iterations. Then I used val() to give a value. But it's not working.

My HTML is

<input type="text" id="shipping:firstname" name="billing[firstname]" value=""/>

My jQuery is

$(window).ready(function(){
var text_to_get='firstname';
var targetID = '#shipping['+text_to_get+']';
$(targetID).val("10");
});

jsFiddle is at: here

Waiting for your help!

Vel Murugan S
  • 580
  • 3
  • 12
  • 31

3 Answers3

4

Try this:

var text_to_get = 'firstname';
var targetID = '#shipping\\:' + text_to_get;
$(targetID).val("10");

You need to escape this : in the targetID since, it will give a error in the console like:

Syntax error, unrecognized expression: unsupported pseudo: firstname

palaѕн
  • 72,112
  • 17
  • 116
  • 136
2
$(window).ready(function(){
    var text_to_get='firstname';
    var targetID = '#shipping\\:'+text_to_get;
    $(targetID).val("10");
});

http://jsfiddle.net/sBqxp/1/

to escape colon char use "\\"
But, I think put colon on ids or names or whatever is a bad idea (use camelCase notation : id="shippingFirtName")

ref: Jquery selecting an ID with a colon in it

Community
  • 1
  • 1
François.M
  • 121
  • 7
0

Here you go!!

    $(document).ready(function(){
      var text_to_get='firstname';
      var targetID = '#shipping:['+text_to_get+']';
      $(targetID).val("10"); 
    }); 
Engineer
  • 5,911
  • 4
  • 31
  • 58