简体   繁体   中英

Setting text within p tag with js variable

I am setting a text value within ap tag to a string being parsed out by splitting. When I alert the variable, I am able to see the text, however when I try to set the text to the variable value, I am unable to see a change. How do I edit this code to set the text within #view_head to the variables I pass within $('#view_head').text(//variable here//)?

The code

 $(edit_but).click(function(e){ //on add input button click
        //e.preventDefault();
        //uses set input data to create variables for use in editor
        var master_string = $('#string_text').text();
        var key_string = master_string.split("|m|");
        var name_string = key_string[0].split("|s|");
        var cont_string = key_string[1].split("|s|");//contains contact variables
        var adr_string = key_string[2].split("|s|");
        var qual_string = key_string[3].split("|s|");//contains description and applying for blob text
        var pos_key_string = key_string[4].split("|s|");
        var edu_key_string = key_string[5].split("|s|");//key string to contain edu strings
        var course_key_string = key_string[6].split("|s|");
        var awd_key_string = key_string[7].split("|s|");
        var skill_key_string = key_string[8].split("|s|");
        var count_key_string ='';


        var pos_string = pos_key_string[0].split("|j|");

        alert (pos_key_string);   //working
        alert (name_string);
        $('#view_head').text(key_string); //not working, does not change
        alert (key_string); //working, can see value in alert for both


        //window.location = '#openModal';
    });

String.split() returns an array, and .text() waits for Type: String or Number or Boolean. - http://api.jquery.com/text/

Try to get it $('#view_head').text(key_string.toString()); or $('#view_head').text(key_string[0]); if data, that you are looking for is in first array element.

如果 text() 不起作用,也许您可​​以尝试 val() 或 html()

Using String .split() returns an Array object, which you cannot insert into an HTML element as text. To create a String from an Array, which can be inserted into an HTML element, use the Array .join() method. EG.

var sample_string = "This is a String";

// split at spaces into an Array
var sample_array = sample_string.split(' ');
//--> ['This','is','a','String']

// join with spaces into a String
var another_string = sample_array.join(' ');
//--> "This is a String"

See MDN : String.prototype.split() & Array.prototype.join()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM