jquery

select input value with jquery


how can I select the content of the word name within the value? In this example it would be hospitality.png

<input type="hidden" id="value_adjunto_certificado_vida_1" name="value_adjunto_certificado_vida_1" value=[{"isImg":true,"name":"hospitalidad.png","type":"image/png","size":100835,"url":"file.php?file=hospitalidad.png&table=curri_inscripcion&field=adjunto_certificado_vida&fkey=b2lvamlpt0fihux&hash=c41c2c0ac34198f0c2fa163355818079","thumbnail_url":"file.php?file=hospitalidad.png&table=curri_inscripcion&field=adjunto_certificado_vida&fkey=b2lvamlpt0fihux&hash=c41c2c0ac34198f0c2fa163355818079&thumbnail=1","isNew":true}]">

Solution

  • You can use like this:

    JSON.parse(document.getElementById("value_adjunto_certificado_vida_1").value)[0].name
    

    But couple of problems though Are you sure your input needs to hold a json value? You have kept it as hidden, so I am guessing you are just using it like a placeholder in some kind of form to store this value. Why not keep the value in different variable and toss it around as per requirement? Are you sure it will always be parsable? I am generally scared of storing RAW jsons as they can break my whole page. More so since I don't see your JSON escaping the special characters. E.g. I would have expected double quotes to be \" and not ".