3

Here is my code.

<script type="text/javascript">
    function showDiv(divID,val) {
        var text= val.value;
        alert(text);
    }
</script>

Mouse:<select onChange="showDiv('#mouse',this);">
    <option value="">Select Mouse Type</option>
    <option value="{'text':'Normal','id':'normalMouse'}">Normal</option>
    <option value="{'text':'Gaming','id':'gamingMouse'}">Gaming</option>
</select>

The output is:

{'text':'Normal','id':'normalMouse'}

But The output should be 'normal' only. That means I want to access to those values separately.How to do that?

1
  • text = JSON.parse(text); alert(text.text); Commented May 1, 2014 at 9:25

3 Answers 3

3

As per w3c org option doc's value can have only string value so it is necessary to convert it into object..


Working fiddle
Javascript code

function showDiv(divID, val) {
    var value = eval('(' + val.value + ')');
    alert('Text: "' + value['text'] + '" Id: "' + value['id'] + '"');
}  

As @Johan say's using eval() is not a safe method.. So for the scenarios where safety is even concerned Working Fiddle using JSON.parse

HTML

<option value='{"text":"Normal","id":"normalMouse"}'>Normal</option>
<option value='{"text":"Gaming","id":"gamingMouse"}'>Gaming</option>  

Javascript

function showDiv(divID, val) {
    var value = JSON.parse(val.value);
    alert('Text: "' + value['text'] + '" Id: "' + value['id'] + '"');
}

Hope it helps..!!

Sign up to request clarification or add additional context in comments.

3 Comments

Never use eval() to parse JSON. In fact, don't use it at all. What if the user inject whatever javascript code he prefers to the attribute and triggers the event?
@bhavik you gave me the perfect answer. thanks a lot
@johan yes thats true. but I just wanted to demonstrate this only. So both of you gave me the correct answer. I dont consider security at all here.
0

Since you're saving an object in string format, how about parsing it first:

var o = JSON.parse(val.value);
console.log(o.text, o.id);

Though single quotes aren't valid for JSON keys nor properties, so I'd suggest that you store your extra data with the valid html5 data attribute e.g.

<option value="something" data-text="Normal" data-id="normalMouse">
    Normal
</option>

And access it using

var text = val.getAttribute('data-text'); // etc...

3 Comments

Or he can do JSON.parse(val.value.replace(/'/g,'"'))
@BatuZet True, allthough I prefer the latter example. If he wants to store complex objects associated with an element, jQuery's data method is the way to go, IMO.
@johan Thank you for the answer. I'll try it now and get back to you. :)
0

Convert ' to ", and then parse it to Json Format

<script type="text/javascript">
function showDiv(divID, val) {
    var text        = val.value,
        // convert ' to "
        jsonStrText = text.replace(/'/g, '"'),
        // parse to json
        jsonText    = JSON.parse(jsonStrText);

    alert(jsonText.text);
}
</script>

Or you can modify html to

<option value="{\"text\":\"Normal\",\"id\":\"normalMouse\"}">Normal</option>

And Directly parse to Json Format

<script type="text/javascript">
function showDiv(divID, val) {
    var text        = val.value,
        // directly parse to json
        jsonText    = JSON.parse(text);

    alert(jsonText.text);
}
</script>

Comments

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.